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 › expand() arrays of objects
Page Index Toggle Pages: 1
expand() arrays of objects (Read 399 times)
expand() arrays of objects
Feb 14th, 2008, 2:03pm
 
Does the expand() method works with self-defined objects classes ? I'm not sure...

//
mystuff[] stuff=new mystuff[0];
stuff = expand (mystuff, mystuff.length+1);

class mystuff{
 mystuff(){
 }
}
Re: expand() arrays of objects
Reply #1 - Feb 14th, 2008, 2:24pm
 
see Processing reference :
http://processing.org/reference/expand_.html

Quote:
When using an array of objects, the data returned from the function must be cast to the object array's data type. For example: SomeClass[] items = (SomeClass[]) expand(originalArray).


That means you must write :
stuff = (myStuff[])expand(mystuff, mystuff.length + 1);
Re: expand() arrays of objects
Reply #2 - Feb 15th, 2008, 9:32pm
 
Thank you. It seems to work fine.
Re: expand() arrays of objects
Reply #3 - Feb 19th, 2008, 4:20pm
 
Hi,

I'm struggling with a similar problem... but this advice doesn't seem to work for me. Most probably because I'm just getting started with Processing and I'm making a basic mistake.

blob[] orb;
numblobs = 180;

void setup()
{
orb = new blob[numblobs];
}

//then, to create an extra 'orb' i do this (which is not the same as the instructions above)
orb = (blob[])expand(orb, orb.length + 1);

//... but when i then try to actually create the new orb, i get an array index out of bounds error.

int n = orb.length;
orb[n] = new blob(etc...)

Hope the truncated version makes sense... if not - here's a link to the full source (the error appears when you left-click on one of the orbs):
http://www.scatterbattery.com/temp/amoebas_1i.pde.txt

Hope someone can help - I have been banging my head against this for the last day or so.

Many thanks,

Mark
Re: expand() arrays of objects
Reply #4 - Feb 19th, 2008, 5:29pm
 
that's because the first element in an array has index 0, and the last has index : length-1

Code:
orb[orb.length-1] = new blob(etc...); 

Re: expand() arrays of objects
Reply #5 - Feb 19th, 2008, 5:45pm
 
haha - so simple, I couldn't see it!

works fine now - thanks very much!
Page Index Toggle Pages: 1