簡體   English   中英

如何在 Java 中對二維數組進行深拷貝?

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

我只是在我的 2d boolean數組上使用.clone() ,認為這是一個深層副本。

如何執行boolean[][]數組的深層復制?

我應該遍歷它並執行一系列System.arraycopy嗎?

是的,您應該遍歷 2D 布爾數組以對其進行深度復制。 如果您使用的是 Java 6,還可以查看java.util.Arrays#copyOf方法。

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

在 Java 8 中,這可以使用 lambda 作為單行代碼完成:

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

我是 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);

我設法想出了一個遞歸數組深拷貝。 即使對於具有不同維度長度的多維數組,它似乎也能很好地工作,例如

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

這是實用方法。

@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);
}

編輯:更新了代碼以使用原始數組。

是的,這是唯一的方法。 java.util.Arrays commons-lang 都沒有為數組提供深度復制。

您可以遍歷此數組並執行一系列Arrays.copyOf方法調用:

boolean[][] arr1 = {{true, true}, {false, true}};    // original array
boolean[][] arr2 = Arrays.copyOf(arr1, arr1.length); // shallow copy
boolean[][] arr3 = Arrays.stream(arr1)               // deep copy
        .map(arr -> Arrays.copyOf(arr, arr.length))
        .toArray(boolean[][]::new);

arr1[0][0] = false;

System.out.println(Arrays.deepToString(arr1)); // [[false, true], [false, true]]
System.out.println(Arrays.deepToString(arr2)); // [[false, true], [false, true]]
System.out.println(Arrays.deepToString(arr3)); // [[true, true], [false, true]]

或者你可以調用Object.clone方法:

boolean[][] arr3 = Arrays.stream(arr1)
        .map(boolean[]::clone)
        .toArray(boolean[][]::new);

或者您可以為此目的創建一個通用方法

static <T> T[][] deepCopy(T[][] matrix) {
    return Arrays.stream(matrix)
            .map(arr -> arr.clone())
            .toArray(s -> matrix.clone());
}

另請參閱:在二維數組的情況下,為什么 Array.copyOf() 會改變原始數組?

這是一個使用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;
    }
}

Java.util.Arrays.deepEquals()

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM