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 › Initializing and array of a class. Help please!
Page Index Toggle Pages: 1
Initializing and array of a class. Help please! (Read 1240 times)
Initializing and array of a class. Help please!
Nov 5th, 2009, 1:59pm
 
I need to initialize an array of a class.  The problem is that the length of the array will be dynamic.  I still need to call the class's constructor with parameters when each new instance is created.  Can someone help me out with the basic syntax/structure of how I would do this?
Re: Initializing and array of a class. Help please!
Reply #1 - Nov 5th, 2009, 2:53pm
 
It is better to use an ArrayList if the length is dynamic.
Simple example:
Code:
ArrayList al = new ArrayList();
for (int i = 0; i < STUFF_NB; i++)
{
Stuff st = new Stuff(param1, param2);
al.add(st);
}

That's all.
Get nth item with Stuff st = (Stuff) al.get(n);
Re: Initializing and array of a class. Help please!
Reply #2 - Nov 5th, 2009, 2:53pm
 
i am not sure if i understand you correctly but wouldnt
nameofyourclassarray.length() work?

or will the length of the array change on runtime? if so, maybe arraylist would be an alternative
http://processing.org/learning/topics/arraylistclass.html
Page Index Toggle Pages: 1