Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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 920 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:
1  
2  
3  
4  
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

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!

http://wiki.java.net/bin/view/Games/JeffFAQ
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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (9 views)
2014-11-28 16:25:09

PocketCrafter7 (10 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (67 views)
2014-11-26 15:20:36

toopeicgaming1999 (16 views)
2014-11-26 15:20:08

SHC (30 views)
2014-11-25 12:00:59

SHC (28 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
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!