This week's book giveaway is in the Agile and Other Processes forum.We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!See this thread for details.
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# Multidimensional array copy

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
Could someone explain this and tell me what is the right way to clone a multidimensional array:

Output: -1

Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
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??

Wanderer
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
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 ]

 please buy this thing and then I get a fat cut of the action: a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com