Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 objects into a Vector?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Ashi_Mashi2




PostPosted: 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
Sponsor
sponsor
wtd




PostPosted: 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




PostPosted: Sun Jan 08, 2006 1:18 pm   Post subject: (No subject)

thankx..works perfect:)
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: