System.arraycopy and the copies that are not copies

Greetings. This is not exactly a java gaming question, but since it still involves a java-jogl project, I figure I might try and ask here.

System.arraycopy is a method for copying the contents of one array to another one, and that should be self-evident. It’s supposed to produce reference-independent duplication of arrays. We all know that having two arrays, one empty and another one filled, and writing second=first will simply turn “second” into a second (no pun intended) name for “first”. By using arraycopy, instead, one should be able to have two fully independent arrays with the same contents.

But it doesn’t seem to work for me. I use multi-dimensional arrays to store vertexes for a 3d scene in jogl, and I’d thought I could replicate objects by copying the arrays containing their vertexes. Specifically, I’d do things like:

float[][][] cube = new float[6][4][3]; //6 faces, 4 vertexes each face, 3 coordinates (xyz) each vertex.
cube = fillCubeCoordinates();

then I’d go

float[][][] anotherCube = new float[6][4][3];
System.arraycopy(cube, 0, anotherCube, 0, cube.length);

And then I’d modify this new array:

for (int face = 0; face < 6; face++)
{
anotherCube[face][0][0] += 1.0f; //shear the bottom-left vertex of each face on the x coordinate
}

Now, what I would expect is that, when I’ll run these arrays through the jogl methods to create polygons, they will produce two distinct objects, a proper cube and a sheared one.
Fact is, though, that they will end up having the same contents, those of the sheared cube, as though anotherCube was a second name for cube, just as if I hadn’t used System.arraycopy.

This also happens when copying contents within the same array, and leaves me baffled and puzzled. Does anybody know if I’m doing something wrong or have forgotten something?

Thanks in advance.

Remember that Java does not have multi-dimensional arrays. What you have there is an array of arrays of arrays of floats. It’s working properly, making a copy of the array of arrays of floats - which of course is an object reference to the same array object as the second “dimension” of your original “outter” array.

What you need to do is write a couple loops to use System.arraycopy() only on the “inner most” set of arrays.

SWP is right. Just to explain in a bit more detail…

An array is an object in Java. When I say byte[] foo = byte[5] I am creating a Java object that holds the 5 byte values. Now when I say Object[] bar = new Object[5] I am creating a Java object which contains 5 object references.

If for instance I have the following:


Object obj1 = new Object();
Object obj2 = new Object();
Object obj3 = new Object();
Object[] objarray1 = new Object[] {obj1,obj2,obj3};

Then objarray1 is an object that contains 3 object references, to the 3 objects.

When you create a second 3 element obj array and do a System.arraycopy from the first to the second, it copies the contents of the first array to the second. Since the contents is references, what you get is a second array that has as its elements references to the same 3 objects.

So since arrays are obejcts. A two dimensional array is really a one dimensional array full of references to one dimensional array objects. If you do a System.arraycopy on it all you will get is another object that contains the same references to the same one dimensional array objects.

I hope that helps.

JK

Thanks to both. I think this solved my problem. While I did know that an array is an object and that multi-dim arrays are actually nested arrays, I didn’t make the next step and connect the two facts. I’ll have to decide whether I prefer the to do the loops to copy the arrays or just fill another one up, but now that I’m not dealing with a “mystery” anymore, it’ll be easier.
Thanks again. :slight_smile: