File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes arrayList again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrayList again" Watch "arrayList again" New topic

arrayList again

brian yuen

Joined: Oct 20, 2003
Posts: 20
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

Joe Ess

Joined: Oct 29, 2001
Posts: 9191

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 ]

[How To Ask Questions On JavaRanch]
brian yuen

Joined: Oct 20, 2003
Posts: 20
Thank you very much, i got it now.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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.
I agree. Here's the link:
subject: arrayList again
jQuery in Action, 3rd edition