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:

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?

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.

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.

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

Got a question about Java and game programming? Just new to the Java Game Development Community? Try my FAQ. Its likely you'll learn something!

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.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org