8

我有两个具有推断大小的多维数组(实际上它们只是 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);吐出一个错误。为什么?:|

4

6 回答 6

13
/**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<rows;row++){
        newArray[row]=(boolean[]) array[row].clone();
    }

    return newArray;
}
于 2009-05-01T19:11:37.753 回答
3

您可能想查看java.util.Arrays.deepEqualsjava.util.Arrays.equals方法。

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

于 2008-10-14T08:48:30.537 回答
3

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

于 2008-10-14T09:00:31.043 回答
1

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

那是因为您正在使用new Character[original.length][];.

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

于 2008-10-14T09:00:33.320 回答
0

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

public static <T extends Serializable> 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]]]
于 2014-08-07T17:47:03.770 回答
-1

我在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();
于 2008-10-14T09:23:50.480 回答