如何在Java中对二维数组进行深层复制?

时间:2021-08-02 21:32:57

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

我只是在我的2d布尔数组上使用.clone(),认为这是一个深层副本。

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

如何执行boolean [] []数组的深层复制?

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

我应该循环它并做一系列的System.arraycopy吗?

6 个解决方案

#1


48  

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.

是的,您应该迭代2D布尔数组以进行深度复制。如果您使用的是Java 6,请查看java.util.Arrays#copyOf方法。

I would suggest the next code for Java 6:

我会建议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;
}

#2


7  

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:

我是Arrays实用程序的粉丝。它有一个copyOf方法,可以为你做一维数组的深度复制,所以你需要这样的东西:

//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);

#3


7  

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.

编辑:更新了代码以使用原始数组。

#4


5  

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

是的,这是唯一的方法。 java.util.Arrays不是commons-lang都不提供数组的深层复制。

#5


5  

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

在Java 8中,这可以使用lambdas实现为单行:

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

#6


0  

Here's a reflective example using java.lang.reflect.Array which is more robust and a bit easier to follow. This method will copy any array, and deeply copies multidimensional arrays.

这是一个使用java.lang.reflect.Array的反射示例,它更健壮,更容易理解。此方法将复制任何数组,并深度复制多维数组。

package mcve.util;

import java.lang.reflect.*;

public final class Tools {
    private Tools() {}
    /**
     * Returns a copy of the specified array object, deeply copying
     * multidimensional arrays. If the specified object is null, the
     * return value is null. Note: if the array object has an element
     * type which is a reference type that is not an array type, the
     * elements themselves are not deep copied. This method only copies
     * array objects.
     *
     * @param  array the array object to deep copy
     * @param  <T>   the type of the array to deep copy
     * @return a copy of the specified array object, deeply copying
     *         multidimensional arrays, or null if the object is null
     * @throws IllegalArgumentException if the specified object is not
     *                                  an array
     */
    public static <T> T deepArrayCopy(T array) {
        if (array == null)
            return null;

        Class<?> arrayType = array.getClass();
        if (!arrayType.isArray())
            throw new IllegalArgumentException(arrayType.toString());

        int length = Array.getLength(array);
        Class<?> componentType = arrayType.getComponentType();

        @SuppressWarnings("unchecked")
        T copy = (T) Array.newInstance(componentType, length);

        if (componentType.isArray()) {
            for (int i = 0; i < length; ++i)
                Array.set(copy, i, deepArrayCopy(Array.get(array, i)));
        } else {
            System.arraycopy(array, 0, copy, 0, length);
        }

        return copy;
    }
}

#1


48  

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.

是的,您应该迭代2D布尔数组以进行深度复制。如果您使用的是Java 6,请查看java.util.Arrays#copyOf方法。

I would suggest the next code for Java 6:

我会建议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;
}

#2


7  

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:

我是Arrays实用程序的粉丝。它有一个copyOf方法,可以为你做一维数组的深度复制,所以你需要这样的东西:

//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);

#3


7  

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.

编辑:更新了代码以使用原始数组。

#4


5  

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

是的,这是唯一的方法。 java.util.Arrays不是commons-lang都不提供数组的深层复制。

#5


5  

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

在Java 8中,这可以使用lambdas实现为单行:

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

#6


0  

Here's a reflective example using java.lang.reflect.Array which is more robust and a bit easier to follow. This method will copy any array, and deeply copies multidimensional arrays.

这是一个使用java.lang.reflect.Array的反射示例,它更健壮,更容易理解。此方法将复制任何数组,并深度复制多维数组。

package mcve.util;

import java.lang.reflect.*;

public final class Tools {
    private Tools() {}
    /**
     * Returns a copy of the specified array object, deeply copying
     * multidimensional arrays. If the specified object is null, the
     * return value is null. Note: if the array object has an element
     * type which is a reference type that is not an array type, the
     * elements themselves are not deep copied. This method only copies
     * array objects.
     *
     * @param  array the array object to deep copy
     * @param  <T>   the type of the array to deep copy
     * @return a copy of the specified array object, deeply copying
     *         multidimensional arrays, or null if the object is null
     * @throws IllegalArgumentException if the specified object is not
     *                                  an array
     */
    public static <T> T deepArrayCopy(T array) {
        if (array == null)
            return null;

        Class<?> arrayType = array.getClass();
        if (!arrayType.isArray())
            throw new IllegalArgumentException(arrayType.toString());

        int length = Array.getLength(array);
        Class<?> componentType = arrayType.getComponentType();

        @SuppressWarnings("unchecked")
        T copy = (T) Array.newInstance(componentType, length);

        if (componentType.isArray()) {
            for (int i = 0; i < length; ++i)
                Array.set(copy, i, deepArrayCopy(Array.get(array, i)));
        } else {
            System.arraycopy(array, 0, copy, 0, length);
        }

        return copy;
    }
}