Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (788)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (860)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  System.arraycopy and the copies that are not copies  (Read 2500 times)
0 Members and 1 Guest are viewing this topic.
Offline initDisplay

Senior Newbie

« Posted 2005-12-18 02:30:42 »

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.
Offline swpalmer

JGO Coder

Exp: 12 years

Where's the Kaboom?

« Reply #1 - Posted 2005-12-18 02:48:08 »

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.

Offline Jeff

JGO Coder

Got any cats?

« Reply #2 - Posted 2005-12-18 04:21:17 »

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.


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!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline initDisplay

Senior Newbie

« Reply #3 - Posted 2005-12-18 10:13:04 »

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. Smiley
Pages: [1]
  ignore  |  Print  

hadezbladez (2078 views)
2018-11-16 13:46:03

hadezbladez (796 views)
2018-11-16 13:41:33

hadezbladez (2047 views)
2018-11-16 13:35:35

hadezbladez (412 views)
2018-11-16 13:32:03

EgonOlsen (3441 views)
2018-06-10 19:43:48

EgonOlsen (3662 views)
2018-06-10 19:43:44

EgonOlsen (2269 views)
2018-06-10 19:43:20

DesertCoockie (2990 views)
2018-05-13 18:23:11

nelsongames (3057 views)
2018-04-24 18:15:36

nelsongames (3723 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20 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‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!