Straightfw
Straightfw

Reputation: 2211

Copying arrays - does the initial size even matter?

So I'm new to Java so this question may look a little silly, probably, but anyway... Suppose we have a code like this:

int[] oneArray = {1,2,3,4,5,6,7,8,9,10};
int[] anotherOne = new int[150];
anotherOne = Arrays.copyOf(oneArray, oneArray.length);

When I print the anotherOne, no matter what the initial size was, after the copyOf method it will be what we stated as the second parameter of the said function. In the case above, the array will shrink to 10 elements (or the pointer will start pointing to another place in the memory, where a new, 10-element array was created, I suppose?). So do I get it wrong or the initial size of anotherOne has no significance at all and if it's created to just become a copy at some point in the future, it should rather be initialized without specifying the size (int[] anotherOne;)?

Upvotes: 1

Views: 147

Answers (4)

pb2q
pb2q

Reputation: 59627

Arrays.copyOf is returning a new array that has been created within that method. You assign that new array to the variable anotherOne, losing the reference to the array that you've created, which will then be eligible for garbage collection.

So the size that you use to create an array with int[] anotherOne = new int[150]; doesn't matter, nor does your initialization or the array that you create with it.

You might as well use:

int[] anotherOne = Arrays.copyOf(oneArray, oneArray.length);

Or:

int[] anotherOne;

// ...
anotherOne = Arrays.copyOf(oneArray, oneArray.length);

If you really need the result to have at least 150 elements, you can also make that happen with Array.copyOf:

int[] anotherOne =
    Arrays.copyOf(oneArray, (oneArray.length > 150) ? oneArray.length() : 150);

Now anotherOne will have at least 150 elements.

Upvotes: 2

Victor
Victor

Reputation: 8490

No.

When you do something like

int[] one = initilize...
int[] two = initilize...
two = one;

The initial value of one is handled by Garbage Collector

Upvotes: 1

nbarraille
nbarraille

Reputation: 10023

Arrays.copyOf already makes a copy. You don't need to initialize anotherOne before that:

int[] oneArray = {1,2,3,4,5,6,7,8,9,10};
int[] anotherOne = Arrays.copyOf(oneArray, oneArray.length);

What happens with your code is that

  • oneArray creates an array in memory
  • anotherOne creates an array of 150 integer in memory
  • the Arrays.copyOf method creates a third array, and makes the variable anotherOne point to this array. The result of this is that there is no more reference to your array of 150 elements, so it will be garbage collected.

That means, in practice, your code will work perfectly, but you are using CPU time (and memory for a short time) for creating and erasing an array of 150 integers for nothing.

Upvotes: 6

Lukas Eder
Lukas Eder

Reputation: 221106

Do this, instead. This will save you from creating an unneeded intermediary array.

int[] oneArray = {1,2,3,4,5,6,7,8,9,10};
int[] anotherOne = Arrays.copyOf(oneArray, oneArray.length);

If, however, you really need your second array to be of length 150, then you can write this:

int[] oneArray = {1,2,3,4,5,6,7,8,9,10};
int[] anotherOne = new int[150];
System.arraycopy(oneArray, 0, anotherOne, 0, oneArray.length);

Upvotes: 9

Related Questions