Forums Register Login

Getting Objects from Collections

+Pie Number of slices to send: Send
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?
Thanks!
+Pie Number of slices to send: Send
No. You have to cast the object when you get it out of the collection.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Great, thanks alot guys, I got it to work like this:

Water proof donuts! Eat them while reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 826 times.
Similar Threads
Doubt in Generics
An object to hold several arrays
ArrayList, clone, and primitive boxed values: shallow copy acting like a deep one!
Most common costliest Elements of Java
Collections
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:56:18.