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 Question about Copying Arrays

Hello, is it possible to copy over only a portion of an array at a time? For example, you have an array A that contains the max number of elements possible and want to copy it over to B in 1000 element chunks. Is this possible?

1 Answer

short[] arrayA = new short[] {1, 2, 3, 4, 5, 6, 7, 8, 9};

// copy arrayA to arrayB from index 0 to 3
short[] arrayB = Arrays.copyOfRange(arrayA, 0, 3);

But you can't extend arrays. So you won't be able to copy other chunks of arrayA into arrayB after that. One way around would be to initialize the arrayB of the same size as arrayA with dummy values, and then copy chuncks of data, BUT this doesn't feel like the solution to any/most problems, seems error prone or at least unnecessarily complex as you would need be careful not use the second part of arrayB.

I hope I understood you question well...