在Java中深度克隆多维数组……?

我有两个多维数组(实际上它们只是2D),它们具有推断的大小。 我如何深度克隆它们? 这是我到目前为止所得到的:

public foo(Character[][] original){ clone = new Character[original.length][]; for(int i = 0; i < original.length; i++) clone[i] = (Character[]) original[i].clone(); } 

等于original.equals(clone);测试original.equals(clone); 吐出一个假的。 为什么? :|

 /**Creates an independent copy(clone) of the boolean array. * @param array The array to be cloned. * @return An independent 'deep' structure clone of the array. */ public static boolean[][] clone2DArray(boolean[][] array) { int rows=array.length ; //int rowIs=array[0].length ; //clone the 'shallow' structure of array boolean[][] newArray =(boolean[][]) array.clone(); //clone the 'deep' structure of array for(int row=0;row 

您可能想要查看java.util.Arrays.deepEquals和java.util.Arrays.equals方法。

我担心数组对象的equals方法执行浅层比较,并且不正确(至少在这种情况下)比较内部Character数组。

数组上的equals()方法是在Object类中声明的方法。 这意味着如果对象相同,它将仅返回true。 同样,它在内容中意味着不一样,但在MEMORY中也是如此。 因此,当您复制内存中的结构时,数组上的equals()将永远不会返回true。

等于original.equals(clone)的测试; 吐出一个假的。 为什么? :|

那是因为你用new Character[original.length][];创建一个新的数组new Character[original.length][];

Arrays.deepEquals(original,clone)应该返回true。

与@Barak解决方案(序列化和反序列化)相同的例子(因为有些人无法理解和向下投票)

 public static  T deepCopy(T obj) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); // Beware, this can throw java.io.NotSerializableException // if any object inside obj is not Serializable oos.writeObject(obj); ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(baos.toByteArray())); return (T) ois.readObject(); } catch ( ClassNotFoundException /* Not sure */ | IOException /* Never happens as we are not writing to disc */ e) { throw new RuntimeException(e); // Your own custom exception } } 

用法:

  int[][] intArr = { { 1 } }; System.out.println(Arrays.deepToString(intArr)); // prints: [[1]] int[][] intDc = deepCopy(intArr); intDc[0][0] = 2; System.out.println(Arrays.deepToString(intArr)); // prints: [[1]] System.out.println(Arrays.deepToString(intDc)); // prints: [[2]] int[][] intClone = intArr.clone(); intClone[0][0] = 4; // original array modified because builtin cloning is shallow System.out.println(Arrays.deepToString(intArr)); // prints: [[4]] System.out.println(Arrays.deepToString(intClone)); // prints: [[4]] short[][][] shortArr = { { { 2 } } }; System.out.println(Arrays.deepToString(shortArr)); // prints: [[[2]]] // deepCopy() works for any type of array of any dimension short[][][] shortDc = deepCopy(shortArr); shortDc[0][0][0] = 4; System.out.println(Arrays.deepToString(shortArr)); // prints: [[[2]]] System.out.println(Arrays.deepToString(shortDc)); // prints: [[[4]]] 

我在jGuru上找到了克隆多维数组的答案 :

 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(this); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); Object deepCopy = ois.readObject();