Page 1 of 1

About Question enthuware.ocajp.i.v7.2.878 :

Posted: Mon Dec 02, 2013 5:00 pm
by javaman
int[] scores = { 1, 2, 3, 4, 5, 6};
System.arraycopy(scores, 2, scores, 3, 2);
for(int i : scores) System.out.print(i);

I don't understand how the correct answer can be 123346. Signature for arraycopy is:
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
System.arraycopy(scores, 2, scores, 3, 2) is in English: copy 1 (2-1) item from the 3rd position (index 2) of the 'scores' array to the 4th (index 3) position of the same array.
So from 1,2,3,4,5,6 I copy item with value 3 to position 4 and I get 1,2,3,3,4,5,6. Array is immutable and I assume that 6 'falls off'. This gives me 1,2,3,3,4,5 and not 1,2,3,3,4,6

Re: About Question enthuware.ocajp.i.v7.2.878 :

Posted: Mon Dec 02, 2013 5:17 pm
by javaman
I guess it copies the nr of chars from the pos indicated by 2nd arguments to position indicated by 4th element and overwrites whatever is there...

Re: About Question enthuware.ocajp.i.v7.2.878 :

Posted: Mon Dec 02, 2013 7:55 pm
by admin
Your code prints 123346 because from the source elements are 3 and 4. You are putting them in position 4, 5, which makes the final array 123 34 6.