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
About Question enthuware.ocajp.i.v7.2.878 :
Moderator: admin
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.878 :
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...
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.878 :
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 144 guests