How do I treat an object I get out of a Vector (collection) as the type I put into it?

John Zukowski

When you get an object out of a Vector (or any collection), the object is returned as being of type Object. You need to cast it back into the object type you put into the data structure if you need to call or treat the object as the original type.

For instance, if you add an array to a vector:

String args[] = {"1", "2", "3"};
Vector v = new Vector();
Then, when you get the object out of the vector, you need to cast it back to the original type:
String args2[] = (String[])v.firstElement();
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact