Deep cloning of multidimensional arrays in Java ...?

I have two multidimensional arrays (well, in fact they are only 2D) that have an inferred size. How can I clone them deeply? Here is what I got so far:

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

Equality Test original.equals(clone); spits out a lie. Why ?: |

+6
java arrays clone
Oct 14 '08 at 8:34
source share
6 answers
 /**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; } 
+13
May 01, '09 at 19:11
source share

You might want to check out java.util.Arrays.deepEquals and java.util.Arrays.equals .

I am afraid that the equals method for array objects does a shallow comparison and does not correctly (at least for this case) compare the internal arrays of Character .

+3
Oct 14 '08 at 8:48
source share

The equals () method for arrays is a class declared in the Object class. This means that it will return true only if the object is the same. In addition, this does not mean the same in CONTENT, but the same in MEMORY. That way, equals () on your arrays will never return true, since you are duplicating a structure in memory.

+3
Oct 14 '08 at 9:00
source share

Equality test original.equals (clone); spits out false. Why ?: |

thats because you are creating a new array with new Character[original.length][]; .

Arrays.deepEquals(original,clone) should return true.

+1
Oct 14 '08 at 9:00
source share

Same as @Barak's solution (serialize and deserialize) with examples (as some people could not understand and vote for it)

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

Using:

  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]]] 
0
Aug 07 '14 at 17:47
source share

I found this answer for cloning multidimensional arrays on 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(); 
-one
Oct. 14 '08 at 9:23
source share



All Articles