2008-10-14 32 views
6

Tôi có hai mảng đa chiều (thực ra chúng chỉ là 2D) có kích thước suy luận. Làm thế nào để tôi sao chép chúng? Dưới đây là những gì tôi đã nhận được cho đến thời điểm này:Nhân bản sâu mảng đa chiều trong Java ...?

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

Thử nghiệm cho sự bình đẳng original.equals(clone); phun ra sai. Tại sao? : |

Trả lời

3

Bạn có thể muốn xem các phương thức java.util.Arrays.deepEqualsjava.util.Arrays.equals.

Tôi sợ phương pháp equals đối với đối tượng mảng thực hiện so sánh nông và không đúng (ít nhất là đối với trường hợp này) so sánh các mảng bên trong Character.

3

bằng phương thức() trên mảng là phương thức được khai báo trong lớp Đối tượng. Điều này có nghĩa là nó sẽ chỉ trả về true nếu đối tượng giống nhau. Tương tự, nó không giống nhau trong CONTENT, nhưng giống nhau trong MEMORY. Như vậy bằng() trên mảng của bạn sẽ không bao giờ trở lại đúng như bạn đang nhân bản cấu trúc trong bộ nhớ.

1

Kiểm tra tính bình đẳng original.equals (bản sao); phun ra sai. Tại sao? : |

đó là do bạn đang tạo mảng mới với new Character[original.length][];.

Arrays.deepEquals(original,clone) phải trả về giá trị đúng.

-1

Tôi tìm thấy câu trả lời này cho cloning mảng đa chiều trên 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(); 
+0

Kỹ thuật này hoạt động cho các mảng đa chiều. Chỉ cần sử dụng 'mảng' thay cho' this' –

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; 
} 
+0

Cảm ơn bạn vì điều này! –

0

Tương tự như giải pháp @Barak (Serialize và Deserialize) với các ví dụ (như một số người không thể hiểu và xuống bình chọn đó)

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

Cách sử dụng:

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