【问题标题】:Deep cloning multidimensional arrays in Java...? [duplicate]在Java中深度克隆多维数组......? [复制]
【发布时间】:2010-09-17 01:19:42
【问题描述】:

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

【问题讨论】:

    标签: java arrays clone


    【解决方案1】:

    与@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]]]
    

    【讨论】:

      【解决方案2】:
      /**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;
      }
      

      【讨论】:

        【解决方案3】:

        我在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();
        

        【讨论】:

        • 这种技术适用于多维数组。只需使用array 代替this
        【解决方案4】:

        平等测试 original.equals(克隆);吐出一个 错误的。为什么? :|

        那是因为您正在使用new Character[original.length][]; 创建一个新数组。

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

        【讨论】:

          【解决方案5】:
          数组上的

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

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

              猜你喜欢
              • 2012-02-24
              • 2019-05-09
              • 2013-04-17
              • 2013-09-17
              • 1970-01-01
              • 2017-09-17
              • 2011-01-18
              相关资源
              最近更新 更多