aspose file tools*
The moose likes Java in General and the fly likes Cloning a Three Dimensional Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cloning a Three Dimensional Array" Watch "Cloning a Three Dimensional Array" New topic
Author

Cloning a Three Dimensional Array

Bill Hogsett
Greenhorn

Joined: Oct 17, 2011
Posts: 9
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()



As you can see I don't use clone() at all.

Thanks.

Bill
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8233
    
  23

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).

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39846
    
  28
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Cloning a Three Dimensional Array