File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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.
 
jQuery in Action, 2nd edition
 
subject: Why should i turn String to String in arrayList ?