【问题标题】:How do I do a deep copy of a 2d array in Java?如何在 Java 中对二维数组进行深拷贝?
【发布时间】:2026-02-16 14:45:01
【问题描述】:

我刚刚在我的二维 boolean 数组上使用了 .clone(),认为这是一个深拷贝。

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

我应该遍历它并执行一系列System.arraycopy's 吗?

【问题讨论】:

    标签: java arrays multidimensional-array copy deep-copy


    【解决方案1】:

    你可以遍历这个数组并执行一系列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);
    

    或者您可以为此创建一个generic method

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

    另见:Why does Array.copyOf() mutate the original array in case of 2D Arrays?

    【讨论】:

    • object.clone 方法是关键——有人发布了通用方法,但很难做一个单行,因为如果你试图在方法之外调用它,使用的变量需要是有效的最终变量)
    【解决方案2】:

    这是一个使用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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      在 Java 8 中,这可以使用 lambdas 以单行方式完成:

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

      【讨论】:

      • 另外,请考虑这里的代码:*.com/a/8200027/109941
      • 我不相信代码很容易适应二维数组,但我很好奇你是否能做到这一点,因为我无法让它工作。
      • @howard 你什么意思?给定的示例深度复制了一个二维数组。
      • 指的是 JIm G 的链接。我仍然不知道如何@JimG 编辑:看起来有效
      【解决方案4】:

      我设法想出了一个递归数组深拷贝。即使对于具有不同维度长度的多维数组,它似乎也能很好地工作,例如

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

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

      【讨论】:

      • 编辑:发现自己:import java.lang.reflect.Array;
      • 这个答案有一个好主意,但不幸的是代码存在一些问题,使其不是一个很好的例子。 (1) array[0].getClass() 使该方法不适用于混合对象数组,例如new Number[] {1, 2.5}。它应该改用array.getClass().getComponentType()。 (2) 使用递归迭代每个数组索引使得该方法不适合/不可预测的大数组。 (3) 无故使用++index 而不是index+1
      • @Radiodef 为什么使用递归使其不适合/不可预测的大型数组?
      【解决方案5】:

      是的,您应该遍历 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;
      }
      

      【讨论】:

      • 请注意,这似乎不适用于Objects。见*.com/questions/15135104/…
      • 之所以有效,是因为您有boolean 的二维数组,这是一种原始类型。但是当你有一个二维对象数组时,这不会复制或克隆对象。请注意,Arrays.copyOf() 本身会进行浅拷贝。
      【解决方案6】:

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

      【讨论】:

      • 请注意,这只会为原始类型创建一个“深拷贝”! Arrays.copyOf() 本身只创建浅拷贝。
      • 请注意,这仅在所有行的列与第一行的列大小相同时才有效。
      【解决方案7】:

      是的,这是唯一的方法。 java.util.Arrays 和 commons-lang 都不提供数组的深拷贝。

      【讨论】: