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;)?
Do this, instead. This will save you from creating an unneeded intermediary array.
If, however, you really need your second array to be of length
150, then you can write this: