My name is Steven and I am studying for the OCA-JP-SE7 (1Z0-803) exam and I am using Enthuware Test Studio for practice questions. I came across a question that has to do with the arraycopy method and am having some trouble understanding exactly what is happening. Here is the code that is used in the question:

class ArrayCopy {
public static void main(String[] args) {
    int[] scores = {1, 2, 3, 4, 5, 6};

    System.arraycopy(scores, 2, scores, 3, 2);
    for(int i : scores) System.out.print(i);

I know the answer is 123346 but I don't get why. Oracle shows this arraycopy(Object src, int srcPos, Object dest, int destPos, int length) which I thought I understood but my incorrect thinking had me choosing 123356 as the answer. I suspect my problem in understanding this is to do with the length part of the method parameter. Can someone help with what am I not getting?

It copies two elements starting with element 2 (NB: zero-based) to positions starting at 3. Ie it copies the values in indexes 2 and 3 (values 3 and 4) to indexes 3 and 4, overwriting the previous values of 4 and 5

Thanks James, Ok I get it now I was not thinking that the length parameter had anything to do with the src but I see now that it does. Which now that I think about it makes sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.