0

I have written following piece of code to check the behavior of System.arraycopy and clone functions. I expect these functions to return a copy of the array but all they are doing is returning a reference to the original array, which is evident from the later part of program where I change the values of original. The copy should not change but it also changes. Please help why its behaving in this way?

public class Testing {

    public static int a[][] = new int[2][2];

    public static void setValueOfA() {
        a[0][0] = 1;
        a[0][1] = 1;
        a[1][0] = 1;
        a[1][1] = 1;
    }

    public static int[][] getValueOfA() {
        int[][] t = new int[2][2];

// Case 1: Not working
//        t = (int[][]) a.clone();
// Case 2: Not working
//        System.arraycopy(a, 0, t, 0, 2);
// Case 3: Working
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                t[i][j] = a[i][j];
            }
        }
        return t;
    }

    public static void main(String[] args) {
        int[][] temp;

        setValueOfA();
        temp = getValueOfA();
        System.out.println("Value of a");
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("Value of temp");
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.print(temp[i][j] + " ");
            }
            System.out.println();
        }

        a[0][0] = 2; a[0][1] = 2; a[1][0] = 2; a[1][1] = 2;

        System.out.println("Value of a");
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("Value of temp");
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.print(temp[i][j] + " ");
            }
            System.out.println();
        }
    }
}
1
  • System.arraycopy does not create anything and it does not return any reference (i.e. it's void). Commented Jan 12, 2015 at 12:01

3 Answers 3

2

I believe (haven't tested it) that System.arraycopy performs a shallow copy of the source array to the target array.

Your call to System.arraycopy is equivalent to :

t[0] = a[0];
t[1] = a[1];

Since a[0] and a[1] are themselves arrays, if you later change a[i][j], you are also changing t[i][j] (since a[i] and t[i] refer to the same array).

Sign up to request clarification or add additional context in comments.

Comments

0

MUN,

You should use Arrays.copyOf() function. See an example:

http://www.tutorialspoint.com/java/util/arrays_copyof_int.htm

/N.

Comments

0

Arrays.copyOf implicitly uses System.arraycopy which does only shallow copy.

To do a deep copy use the traditional way of iterating and assigning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.