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 of object and expand method
Page Index Toggle Pages: 1
Array of object and expand method (Read 508 times)
Array of object and expand method
Mar 5th, 2008, 12:41am
 
Hi,

I am trying to create an array of dynamic objects, which size should be able to increase. I tried ArrayList but it does not seem to take objects, does it?

Anyway, here is my code and the error I get

Code:

Ev[] aEvent = new Ev[ 1];

void setup() {
size(800, 600);
aEvent = expand(aEvent, aEvent.length+1);

}


void draw() {

}



class Ev {

int[] x;
Ev() {
x = new int[100];
};
}


Semantic Error: The type of the right sub-expression, "java.lang.Object", is not assignable to the variable, of type "Temporary_8045_9147$Ev[]".

I must be doing something wrong but I can't figure out what it is. Thank you for your help :)
Re: Array of object and expand method
Reply #1 - Mar 5th, 2008, 9:17am
 
If you use ArrayList you have to cast the objects when you get them.

Code:


class Ball {
//do something here
}

ArrayList balls = new ArrayList();
balls.add(new Ball());
Ball b = (Ball)balls.get(0);//get the first ball
Ball[] bs = balls.toArray(new Ball[balls.size()]); //create an Ball array from your ArrayList
Re: Array of object and expand method
Reply #2 - Mar 5th, 2008, 3:19pm
 
You need a cast to tell the program that what's being returned is really an array of Evs:

Code:
aEvent = (Ev[])expand(aEvent, aEvent.length+1);
Re: Array of object and expand method
Reply #3 - Mar 5th, 2008, 5:19pm
 
Thanks to both of you, that really helps Smiley
Page Index Toggle Pages: 1