This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am taking a Java class. The test yesterday asked for code to deeply copy a 3D array. I had never figured out how to do it. But now I think I have (sort of). I would appreciate comments on my approach and an example using clone()
Bill Hogsett wrote:I am taking a Java class. The test yesterday asked for code to deeply copy a 3D array. I had never figured out how to do it. But now I think I have (sort of). I would appreciate comments on my approach and an example using clone()
Well as far as I can see, your first test proves nothing, since the two arrays would have been deeply equal to begin with (both will be all 0's unless you actually change them to something else).
Also, you might want to check first for them not being '==' before you run Arrays.deepEquals() (to be honest, I don't know whether it does it itself or not, nor what it returns if both arrays refer to the same array; but to my mind two arrays that are == are not clones).
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
There is no such thing as a 3D array. What you have is an array of arrays of arrays.
If you are getting that to work with the Arrays method (which I believe is only available in Java7), then I suggest you avoid the clone() method. You will find more about clone() in Effective Java by Joshua Bloch (old edition), and you will see Bloch doesn’t like that method.