Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes Multidimensional array copy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multidimensional array copy" Watch "Multidimensional array copy" New topic

Multidimensional array copy

Apu Virtanen

Joined: Nov 20, 2005
Posts: 2
Could someone explain this and tell me what is the right way to clone a multidimensional array:

Output: -1
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Though I am not sure but I think clone() method is very heavy. You could write a method by your own that will create the same type of array and copy the contents...

Not sure, it is a good idea or not??
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I wouldn't say clone() is "heavy", no. The problem here is that clone() will create a shallow copy. Apu's code shows how a change to the cloned array actually affects the original as well, which is not desired. That's because while the outer int[][] array is indeed copied, the inner int[] arrays (the rows) are not. The new int[][] just has a bunch of references to the original int[] rows. So changing an element in a row of the copy ends up affecting the row in the original as well, because it's the same row.

So yeah, forget clone() here. As rathi says, you can just create a new "multidimensional array" (actually an array of arrays of arrays...) with the appropriate dimensions, and write a series of nested loops that copies everything over to the new array. That is, assuming you know the type and number of dimensions in the array to begin with. If you want a method that can do a deep copy of any array, that seems to require some reflection:

If you're not using JDK 5, you can omit the copyMultiArray() method, make copyMultiArrayObject() public, and perhaps rename it to copyMultiArray() or whatever you prefer. The only catch is, you'll have to cast the result yourself.

Note that @SuppressWarnings doesn't actually suppress warnings from Sun's JDK 5 compiler, but hopefully it will in the future. Some IDEs such as IntelliJ do recognize @SuppressWarnings now though.

Also note that this isn't a completely deep copy. It copies all levels of the array, but when it gets to the last level it relies on System.arraycopy(). If the component type at that level is a reference type, then we get a shallow copy. Which is fine for immutable types, but maybe not for mutable ones. Unfortunately I don't see a good way to make this work for deep copies without changing strategies completely.

If the lowest-level component type of the array is some sort of Serializable, then one could simply serialize the multiarray and then deserialize it. It may not be the fastest operation, but it's easy to code. Alternately you can grab XStream and copy many types of objects, not just Serializables:

However there are limits, e.g. you usually can't copy objects that require external resources like a FileWriter or a Socket. Hopefully you wouldn't want to copy such objects anyway, right?
[ November 24, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Multidimensional array copy
It's not a secret anymore!