objects into a Vector?
Author |
Message |
Ashi_Mashi2
|
Posted: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sat Jan 07, 2006 2:41 am Post subject: (No 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); |
|
|
|
|
|
|
Ashi_Mashi2
|
Posted: Sun Jan 08, 2006 1:18 pm Post subject: (No subject) |
|
|
thankx..works perfect:) |
|
|
|
|
|
|
|