We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to pass an array of arrays as values
Page Index Toggle Pages: 1
How to pass an array of arrays as values ? (Read 996 times)
How to pass an array of arrays as values ?
Jun 8th, 2005, 5:48pm
 
Hi all,

I'm having little problems here with a very simple function that stores coordinates in a global array. Everytime new coordinates are found, the old ones should be shifted towards the end of the array and the new coordinates should be stored in coords[0]. The problem is that if I store the new coordinates in [0], *every* member of the array gets reset to those new values. I figured out that the coords[0] = newcoords assignment probably passes a reference and not the actual values.

But *how* can I store an array of integers in another array without passing it by reference ? I'm pretty new to java and processing, so please forgive me if this question seems really stupid to you.

Any help is really appreciated !!

Thanks, David


The relevant code looks like this:

for (int i = 20; i>0; --i) {
 coords[i+1] = coords[i]
}
coords[0] = newcoords;

Where coords is a global array of int[20][20][2] and newcoords is int[20][2].

This behaviour can also be demostrated by this code:

int[][] globalvar = new int[2][2];

void setup() {
 noLoop();
}

void draw() {
 int[] localvar = new int[2];
 localvar[0] = 1;
 localvar[1] = 1;
 
 globalvar[0] = localvar; // Why is this passed as reference ?????
 globalvar[1][0] = 2;
 globalvar[1][1] = 2;
 println("before: " + globalvar[0][0] + " " + globalvar[1][0]);
 localvar[0] = 3;
 localvar[0] = 3;
 println("after: " + globalvar[0][0] + " " + globalvar[1][0]);
}  

output:

before: 1 2
after: 3 2
Re: How to pass an array of arrays as values ?
Reply #1 - Jun 8th, 2005, 6:37pm
 
I've just searched through the beta and alpha boards and found  a few threads that describe similar problems. As System.arraycopy() is also mentioned, I've tried that but it doesn't work as well. Maybe it's just that I'm now stuck on this problem for a couple of hours (!) and I cannot see the real problem anymore. Anyway, here's the current code. Maybe someone can figure out a way to do that correctly.

The code still uses the global array globhist and I even tried to copy the array in a new, temporary one. But it doesn't seem to work...


void populateHistory(int[][] currentglobs) {
 int[][][] histtmp = new int[histglobs][maxglobs][2];
 System.arraycopy(globhist, 0, histtmp, 0, histglobs);
 
 // Populate the globhistory
 for (int i = histglobs - 2; i>0; --i) {
     System.arraycopy(histtmp[i+1], 0, globhist[i], 0, maxglobs);
 }
 System.arraycopy(currentglobs, 0, globhist[0], 0, maxglobs);
}
Re: How to pass an array of arrays as values ?
Reply #2 - Jun 8th, 2005, 9:15pm
 
Damnit !
Hours of searching the web and trying out stupid stuff and in the end it was just my debug-routine that was wrong ! ;-)

So to sum up:
System.arraycopy(...) works quite fine for to copying multi-dimensional arrays around (by value and *not* by reference).

I'm going to bed now.

The final code then looks like this:

Code:

// populate the glob history
for (int i = histglobs-2; i>-1; --i) {
System.arraycopy(globhist[i], 0, globhist[i+1], 0, maxglobs);
}
System.arraycopy(globcurr, 0, globhist[0], 0, maxglobs);
Page Index Toggle Pages: 1