Computer Science Canada

objects into a Vector?

Author:  Ashi_Mashi2 [ Sat Jan 07, 2006 2:33 am ]
Post subject:  objects into a Vector?

hi every1....

I want to store an array of objects (i.e. my own classes) into an array and retrieve the address as needed...does anyone have any ideas?

i tried using a vector array, it stores fine, but, when I want to retrieve it using .elementAt (x) it wont return the object.


e.g.
Vector v = new Vector();
Test t = new Test();
v.addElement(t);


ERROR: Test t2 = v.elementAt (0);

thx.

Author:  wtd [ Sat Jan 07, 2006 2:41 am ]
Post subject: 

Since you're using 1.4.2, you'll need to insert a cast. You see, when an object is stored in a Vector, it's stored as simply an object of class Object. Now, you assign it to a variable of class Test. This is now good since an Object is not necessarily a Test object.

code:
Test t = (Test)v.elementAt(0);

Author:  Ashi_Mashi2 [ Sun Jan 08, 2006 1:18 pm ]
Post subject: 

thankx..works perfect:)


: