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 › using vector for array storing
Page Index Toggle Pages: 1
using vector for array storing (Read 420 times)
using vector for array storing
Mar 31st, 2006, 1:26am
 
hello list! im trying  to create an array of objects . i want to create a new object each 6 seconds and add it to my array, looking for information about doing this i discovered that java has 2 mutable structures for storing object: arraylist and vector,
so i decide to use vector.
the idea is really simple. my class creates an animated dot.
so i would like that each 6 seconds a new animated dot (object) appears on my screen and add it(object) to the array.

heres the code, can anybody explain me why it doesnt work?



Vector a= new Vector();

int lastRun;

void setup()
{
 size(200, 200);
 stroke(255);
 framerate(30);
  lastRun=0;
  a.add(new punto(200,200));


}
void draw()
{
background(51);

a.display();



int m=millis();
 if(m>=(lastRun+6000))
 {
    lastRun=m;
    a.add(new punto(200,200));
 
}


}
class punto
 {
   int x;
   int y;
punto( int xa, int ya){

   float xf=random(xa);
float yf=random(ya);
 x= int(xf);
 y= int(yf);
print(x);

   }
 
 void display(){
    x = x - 1;
 if (x < 0) {
   x = height;
 }
   point(x,y);
   }  

}
Re: using vector for array storing
Reply #1 - Mar 31st, 2006, 1:26am
 
ooops sorry the subject should be called "using vector for object storing"
Re: using vector for array storing
Reply #2 - Mar 31st, 2006, 11:00am
 
One of the problems with Vector is that it neither knows nor cares what kind of Object you put in it, so to do anything with an object stored in a vector, you have to cast it to a certain type before you can do anything with it.

For example, if you do
Code:
a.add(new punto(200,200)); 


the Vector doesn't know that it's got a "punto" just an object, so to use the contained punto you'll have to do something like:
Code:
a.add(new punto(200,200));
...
for(int i=0;i<a.size();i++)
{
punto tmp=(punto)a.get(i);
tmp.display();
}


I think this is the syntax in Java.

Unfortunately, or possibly fortunately, it doesn't work like in C++ where a Vector is created to handle a number of a single type of object. You can put in any number of different types of object, and the Vector won't care. You'll have to do some voodoo to find out what type the one you've just retrieved is though.
Page Index Toggle Pages: 1