Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Arrays

Didnt get this arraycopy

So arrays of course i understand this but what exactly arraysofcopry do? please help

1 Answer

Daiane Silva
Daiane Silva
5,668 Points

Well, when you create an array, its a fixed size. So to add another element you have to create a bigger array, copy the content of the old array to this new one.

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

Basically, the first position is the array that you want to copy, the second is the position to start the copy of the array, normally at 0, the third is the array that you're making a copy to, the forth the position you want to send the copy to and the last one is the size of the first array.

It's like a loop that goes through your array, copying to another one:

int[] a = {1,2,3,4}
int[] b = new int[6];
for(int i = 0, i < a.lenght(); i ++{
b[i] = a[i]
}