2008-10-14 10 views
6

Ich habe zwei mehrdimensionale Arrays (naja, eigentlich sind sie nur 2D), die Größe abgeleitet haben. Wie kann ich sie tiefklonen? Hier ist, was ich bisher bekommen:Deep Clonen mehrdimensionaler Arrays in Java ...?

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

Ein Test für die Gleichstellung original.equals(clone); spuckt ein falsch. Warum? : |

Antwort

3

Sie möchten vielleicht die Methoden java.util.Arrays.deepEquals und java.util.Arrays.equals überprüfen.

Ich fürchte, die equals Methode für Array-Objekte führt einen flachen Vergleich, und nicht richtig (zumindest für diesen Fall) vergleicht die inneren Character Arrays.

3

Die equals() -Methode für Arrays ist diejenige, die in der Object-Klasse deklariert ist. Dies bedeutet, dass nur Wahr zurückgegeben wird, wenn die Objekte identisch sind. Gleiches bedeutet nicht dasselbe in CONTENT, aber dasselbe in MEMORY. Daher wird equals() für Ihre Arrays nie als wahr zurückgegeben, da Sie die Struktur im Speicher duplizieren.

1

Ein Test auf Gleichheit original.equals (Klon); spuckt einen false aus. Warum? : |

Das ist, weil Sie ein neues Array mit new Character[original.length][]; erstellen.

Arrays.deepEquals(original,clone) sollte True zurückgeben.

-1

fand ich diese Antwort zum Klonen von mehrdimensionalen Arrays auf 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

Diese Technik funktioniert für mehrdimensionale Arrays. Verwenden Sie 'Array' anstelle von' 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

Vielen Dank dafür! –

0

Gleiche wie @Barak Lösung (serialisiert und Deserialize) mit Beispielen (wie manche Leute nicht gestimmt verstehen konnte und nach unten, dass)

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

Verbrauch:

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