This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why should i turn String to String in arrayList ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why should i turn String to String in arrayList ?" Watch "Why should i turn String to String in arrayList ?" New topic
Author

Why should i turn String to String in arrayList ?

Aladdin Raul
Greenhorn

Joined: May 24, 2011
Posts: 12
Hey everyone, please lend me help understanding in line 7 why should i turn arr.get(indx) to String, If it already returns String, since get() method of ArrayList returns String of an index, thanks a million .

Ronald Castillo
Ranch Hand

Joined: Apr 16, 2011
Posts: 47

Hi Aladdin Raul,

The ArrayList.get() method, given the index, doesn't return a String but an Object
Here's the link to the docs:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/List.html#get(int)

By calling toString() on that Object you will get a textual representation of the object. Really depends on the object and if it implements the toString method or not.
Aladdin Raul
Greenhorn

Joined: May 24, 2011
Posts: 12
You bet, thanks
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Hey Aladdin Raul ,
Ronald Castillo is correct, arr.get returns an Object and label's setText expects a String, so there is a type mismatch. Hence it is necessary to use toString.
I guess you could use generics to your advantage to avoid toString. For instance

would work fine. Give it a try.
 
wood burning stoves
 
subject: Why should i turn String to String in arrayList ?
 
Similar Threads
Placing ArrayList into Object Array
Using Comparable, Generics, and recursion to sort an array alphabetically
Finding Nth Largest element of an array without sorting
Trouble with return value from a JDialog
how to not display a value from dropdown which is populated from database using java