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 & HelpPrograms › Bizarre array behaviour
Page Index Toggle Pages: 1
Bizarre array behaviour (Read 594 times)
Bizarre array behaviour
Apr 1st, 2007, 4:26pm
 
Hi, as part of a program am I writing i set up a 2d array of values, however after setting the values they seem to be going missing. Here is the code in question;

Quote:
for (int z=0; z < numObj; z++) {
 
xtildeArray = new double[numObj][D];
 
xtilde = new double[numValues];
 
for (int i=0; i < D; i++){
   
xtilde[i] = d*objectsArray[z].getValue(i)/nhatdotx;
 
}
 
xtildeArray[z] = xtilde;

println(xtildeArray[z]);
}

println(xtildeArray[0]);


The first println statement within the loop displays the values intended correctly, however the second println outside the loop returns values 0,0,0 for whichever array element I call. The xtildeArray is declared globally, and im baffled as to what is going on. Any help would be greatly appreciated.

Andrew.
Re: Bizarre array behaviour
Reply #1 - Apr 1st, 2007, 5:33pm
 
that's because you are reassigning the array every loop. you have to move that before (outside) the for-loop:

Code:
  
xtildeArray = new double[numObj][D];
for (int z=0; z < numObj; z++)
{
 xtilde = new double[numValues];
 for (int i=0; i < D; i++)
 {
   xtilde[i] = d*objectsArray[z].getValue(i)/nhatdotx;
 }
 xtildeArray[z] = xtilde;
 println(xtildeArray[z]);
}
println(xtildeArray[0]);


F
Re: Bizarre array behaviour
Reply #2 - Apr 1st, 2007, 5:38pm
 
Thanks. Can't believe im making silly mistakes like this and not noticing. Cheers
Re: Bizarre array behaviour
Reply #3 - Apr 1st, 2007, 5:47pm
 
happens to me all the time, just spend 2 hours hunting for a bug. in the end turns out it was the first thing i wrote early this morning ... ouch.

F
Page Index Toggle Pages: 1