• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cloning a Three Dimensional Array

 
Bill Hogsett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic