How do I do a deep copy of a 2d array in Java?

84,359

Solution 1

Yes, you should iterate over 2D boolean array in order to deep copy it. Also look at java.util.Arrays#copyOf methods if you are on Java 6.

I would suggest the next code for Java 6:

public static boolean[][] deepCopy(boolean[][] original) {
    if (original == null) {
        return null;
    }

    final boolean[][] result = new boolean[original.length][];
    for (int i = 0; i < original.length; i++) {
        result[i] = Arrays.copyOf(original[i], original[i].length);
        // For Java versions prior to Java 6 use the next:
        // System.arraycopy(original[i], 0, result[i], 0, original[i].length);
    }
    return result;
}

Solution 2

In Java 8 this can be accomplished as a one-liner using lambdas:

<T> T[][] deepCopy(T[][] matrix) {
    return java.util.Arrays.stream(matrix).map(el -> el.clone()).toArray($ -> matrix.clone());
}

Solution 3

I'm a fan of the Arrays utility. It has a copyOf method that will do a deep copy of a 1-D array for you, so you'd want something like this:

//say you have boolean[][] foo;
boolean[][] nv = new boolean[foo.length][foo[0].length];
for (int i = 0; i < nv.length; i++)
     nv[i] = Arrays.copyOf(foo[i], foo[i].length);

Solution 4

I've managed to come up with a recursive array deep copy. It seems to work pretty well even for multi dimensional arrays with varying dimension lengths e.g.

private static final int[][][] INT_3D_ARRAY = {
        {
                {1}
        },
        {
                {2, 3},
                {4, 5}
        },
        {
                {6, 7, 8},
                {9, 10, 11},
                {12, 13, 14}
        }
};

Here is the utility method.

@SuppressWarnings("unchecked")
public static <T> T[] deepCopyOf(T[] array) {

    if (0 >= array.length) return array;

    return (T[]) deepCopyOf(
            array, 
            Array.newInstance(array[0].getClass(), array.length), 
            0);
}

private static Object deepCopyOf(Object array, Object copiedArray, int index) {

    if (index >= Array.getLength(array)) return copiedArray;

    Object element = Array.get(array, index);

    if (element.getClass().isArray()) {

        Array.set(copiedArray, index, deepCopyOf(
                element,
                Array.newInstance(
                        element.getClass().getComponentType(),
                        Array.getLength(element)),
                0));

    } else {

        Array.set(copiedArray, index, element);
    }

    return deepCopyOf(array, copiedArray, ++index);
}

EDIT: Updated the code to work with primitive arrays.

Solution 5

Yes, that's the only way to do it. Neither java.util.Arrays not commons-lang offer deep copy for arrays.

Share:
84,359
Admin
Author by

Admin

Updated on June 17, 2021

Comments

  • Admin
    Admin almost 3 years

    I just got bit by using .clone() on my 2d boolean array, thinking that this was a deep copy.

    How can I perform a deep copy of my boolean[][] array?

    Should I loop through it and do a series of System.arraycopy's?

  • elgehelge
    elgehelge about 10 years
    Edit: Found out myself: import java.lang.reflect.Array;
  • Timo
    Timo over 9 years
    Note that this does not seem to work for Objects. See stackoverflow.com/questions/15135104/…
  • codepleb
    codepleb almost 9 years
    Note, that this only creates a "deep copy" for primitive types! Arrays.copyOf() itself only creates shallow copies.
  • Jim G.
    Jim G. about 6 years
    Also, consider the code here: stackoverflow.com/a/8200027/109941
  • Radiodef
    Radiodef almost 6 years
    This answer has a good idea, but the code unfortunately has some problems that make it not a very good example. (1) array[0].getClass() makes the method unsuitable for arrays of mixed objects such as e.g. new Number[] {1, 2.5}. It should use array.getClass().getComponentType() instead. (2) Using recursion to iterate each array index makes the method unsuitable/unpredictable for large arrays. (3) Uses ++index instead of index+1 for no reason.
  • howard
    howard over 3 years
    I don't believe that code adopts itself easily to 2d arrays but I'd be curious if you managed to do it since I can't get it to work.
  • SlavaSt
    SlavaSt over 3 years
    @howard What do you mean? The given example deep copies a 2d array.
  • howard
    howard over 3 years
    was referring to JIm G's link. I still don't know how to @JimG Edit: looks like that worked
  • Nil Suria
    Nil Suria over 3 years
    Note too that this only works if the columns of all the rows are the same size as the first.
  • Jesper
    Jesper almost 3 years
    This works because you have 2D array of boolean, a primitive type. But when you have a 2D array of objects, this will not copy or clone the objects. Note that Arrays.copyOf() by itself does a shallow copy.
  • Kröw
    Kröw over 2 years
    @Radiodef why does the use of recursion make it unsuitable/unpredictable for large arrays?