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 › array length not changed after append
Page Index Toggle Pages: 1
array length not changed after append? (Read 1250 times)
array length not changed after append?
May 27th, 2010, 2:05am
 
Hi -
I'm trying to repopulate an array:
void setup(){
size(200,200);
}
void draw(){
int[] a = new int[0];
//expand(a,1);
append (a,1);
text (a.length,100,100);
noLoop();
}

It prints 0, while I'd expect the array length to be 1, after the append operation. Also, trying to access a[0] gives an array boundary error.
What am I doing wrong or where might I be misunderstanding the effect of append?

My actual goal is to have an array that can be repopulated, with a varying number of elements between 'frames'. How do I do that?

Thanks in advance,
wmw.
Re: array length not changed after append?
Reply #1 - May 27th, 2010, 2:23am
 
Let see append() reference...
String[] sa2 = append(sa1, "MA ");
Note that append "Expands an array by one element and adds data to the new position" but it is ambiguous because actually it creates a new array instead of expanding it, so you need to assign the result of append, either to a new array as shown, or to the old array:
a = append(a, 1);
Perhaps they should have stated that more explicitly than just giving an example...
Re: array length not changed after append?
Reply #2 - May 27th, 2010, 2:46am
 
aha! Thanks a bunch, PhiLho! Guess that wasn't indeed quite as obvious from the dox. a = append(a,1); works perfectly!
Regards,
wmw.
Page Index Toggle Pages: 1