Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why should i turn String to String in arrayList ?

 
Aladdin Raul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You bet, thanks
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Java Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic