Deep Copy an Array in Java

Rupam Yadav Jan 30, 2023 Jun 15, 2021
  1. Deep Copy Using the System.arraycopy() Function in Java
  2. Deep Copy an Array Using the Arrays.copyOf Command in Java
Deep Copy an Array in Java

In this tutorial, we discuss how to deep copy an array in Java.

Generally, there are two types of array copy methods in Java: the Shallow Copy and the Deep Copy. In Shallow copy, the objects are copied. On the other hand, all the items are copied while a different array is created in Deep Copy.

Below is an example that shows what happens when we copy an array into another directly. We create an array arr1 with items and then initialize another arr2 with arr1. When any element of arr2 is changed, the change also gets reflected to arr1.

import java.util.Arrays;

public class DeepCopy {
    public static void main(String[] args) {

        int[] arr1 = {10, 20, 30};
        System.out.println("arr1 Before copying: " + Arrays.toString(arr1));


        int[] arr2 = arr1;
        System.out.println("arr2 After copying: " + Arrays.toString(arr1));

        arr2[0] = 30;
        System.out.println("arr1 after copying and changing an element in arr2: " + Arrays.toString(arr1));

    }
}

Output:

arr1 Before copying: [10, 20, 30]
arr2 After copying: [10, 20, 30]
arr1 after copying and changing an element in arr2: [30, 20, 30]

Deep Copy Using the System.arraycopy() Function in Java

In the following example, we take an array arr1 with a few items and then take another array arr2 and give it the size equal to that of arr1. We call the arrayCopy() method of the System class that copies an array to another.

The arrayCopy() function takes four arguments; the first two are the source array and the starting position of copy in the source array. The third argument is the destination array and its starting position, where we’ll copy elements, and the number of items to be copied in a new array.

When we change the arr2 item, just like in the previous example, the elements of arr1 don’t change at all.

import java.util.Arrays;

public class DeepCopy {
    public static void main(String[] args) {

        int[] arr1 = {10, 20, 30};
        System.out.println("arr1 Before copying: " + Arrays.toString(arr1));

        int[] arr2 = new int[arr1.length];

        System.arraycopy(arr1, 0, arr2, 0, arr1.length);
        System.out.println("arr2 After copying: " + Arrays.toString(arr1));

        arr2[0] = 30;
        System.out.println("arr1 after copying and changing an element in arr2: " + Arrays.toString(arr1));

    }


}

Output:

arr1 Before copying: [10, 20, 30]
arr2 After copying: [10, 20, 30]
arr1 after copying and changing an element in arr2: [10, 20, 30]

Deep Copy an Array Using the Arrays.copyOf Command in Java

Below, we use the copyOf() method of the Arrays utility class. It accepts the array to copy and its size then returns the array of the same type. We make a new array arr2 using this method and check if changing arr2 changes arr1 or not. The output shows the result.

Note that this method and the previous one create a shallow copy instead of a deep copy when the array contains objects instead of primitives.

import java.util.Arrays;

public class DeepCopy {
    public static void main(String[] args) {
        
        int[] arr1 = {10, 20, 30};
        System.out.println("arr1 Before copying: "+Arrays.toString(arr1));

        int[] arr2 = Arrays.copyOf(arr1, arr1.length);
        System.out.println("arr2 After copying: "+Arrays.toString(arr1));

        arr2[0] = 30;
        System.out.println("arr1 after copying and changing an element in arr2: "+Arrays.toString(arr1));

    }

}

Output:

arr1 Before copying: [10, 20, 30]
arr2 After copying: [10, 20, 30]
arr1 after copying and changing an element in arr2: [30, 20, 30]

We can also use Arrays.copyOf() to copy two dimensional array. In the example below, we have a 2D array arr. We have a new array arr2, and in the loop, we use Arrays.copyOf that takes every element of arr and copies it into the arr2 item by item. Once copied, we check if arr1 changes if arr2 is changed. If it doesn’t change, it’s a deep copied array.

import java.util.Arrays;

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


        System.out.println("Values of arr");
        for (int i = 0; i < arr.length; i++)
            for (int j = 0; j < arr.length - 1; j++)
                System.out.println("arr[" + i + "][" + j + "] = "
                        + arr[i][j]);

        System.out.println();

        int[][] arr2 = new int[arr.length][];
        for (int i = 0; i < arr.length; i++) {
            arr2[i] = Arrays.copyOf(arr[i], arr[i].length);
        }

        System.out.println("Values of arr2");

        for (int i = 0; i < arr2.length; i++)
            for (int j = 0; j < arr2.length - 1; j++)
                System.out.println("arr2[" + i + "][" + j + "] = "
                        + arr2[i][j]);

        arr2[0][1] = 5;

        System.out.println();

        System.out.println("Values of arr after changing an element in arr2");
        for (int i = 0; i < arr.length; i++)
            for (int j = 0; j < arr.length - 1; j++)
                System.out.println("arr[" + i + "][" + j + "] = "
                        + arr[i][j]);


    }


}

Output:

Values of arr
arr[0][0] = 1
arr[0][1] = 2
arr[1][0] = 3
arr[1][1] = 4
arr[2][0] = 5
arr[2][1] = 6

Values of arr2
arr2[0][0] = 1
arr2[0][1] = 2
arr2[1][0] = 3
arr2[1][1] = 4
arr2[2][0] = 5
arr2[2][1] = 6

Values of arr after changing an element in arr2
arr[0][0] = 1
arr[0][1] = 2
arr[1][0] = 3
arr[1][1] = 4
arr[2][0] = 5
arr[2][1] = 6
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Array