Forums Register Login
Getting Objects from Collections
I'm just wondering, in a Collection of objects, how do you get objects back out in the same type that they went in? Just like in an array where you access it by the a[10].
In the List interface, the get() method returns an Object, not the original type that went in. The Map get() does the same thing when you give it a key as a parameter.
Do you have to convert the collection to an array using toArray() to access the elements?
No. You have to cast the object when you get it out of the collection.
Also you may want to use instanceof to check that the type of object is what you are expecting before you cast it.
[ September 15, 2002: Message edited by: Barry Gaunt ]
Great, thanks alot guys, I got it to work like this:

Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 612 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 20, 2018 22:14:39.