Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrayList again

 
brian yuen
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me what's wrong with the arrayList
the output is like :TestAccount@a18aa2
the class is called TestAccount...
i add it to an arrayList successfully like...

and use the following code to output

Thanks
 
Joe Ess
Bartender
Posts: 9300
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing is wrong with ArrayList. Unless you override toString(), it prints out the name of the object you called it on plus a number (memory location, hash code, I forget). Override TestAccount.toString() to do something meaningful.
[ October 22, 2003: Message edited by: Joe Ess ]
 
brian yuen
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, i got it now.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what it's worth, because of the polymorphic nature of Java it's not necessary to cast the ArrayList elements before printing them out. The following would work just as well:

At run time the system would determine that the element being printed--addTestAcc.get(i)--is a "TestAccount", and call the appropriate "toString()" method.
You only need to do an explicit cast to call a method that's defined in that class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic