aspose file tools
The moose likes Beginning Java and the fly likes User-defined objects in collections Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "User-defined objects in collections" Watch "User-defined objects in collections" New topic
Author

User-defined objects in collections

Sharda Vajjhala
Ranch Hand

Joined: Nov 14, 2001
Posts: 57
I have a program as follows:

This prints out as follows:
Mailing list is:
Address@f56cf041
Address@f574f041
Address@f624f041
Address@f634f041
Why does it not print out the address contained within the element? It prints out the actual elements, if I use simple objects such as Strings
such as the following example:

Output of this is as follows:
C U C K O O
I would have expected the 1st MailingList program to print out the actual addresses(although not formatted properly).
Can anyone please explain?
Thanks
Sharda[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ July 30, 2002: Message edited by: Dirk Schreckmann ]
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
That's because your address class does not override toString() method of Object Class.The toString method for Object class returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.
For example - Address@f56cf041


Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
Stu Glassman
Ranch Hand

Joined: Jul 01, 2002
Posts: 91
When you output a class, the program actually calls MyClass.toString(). The Object class defines toString as returning ClassName@hashcode. In order to output the Address correctly add the following method:

Feel free to change the code to produce whatever formatting you'd like.
-Stu
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: User-defined objects in collections
 
Similar Threads
Reading Elements from an ArrayList, 3 at a time
collection
Enhanced for loop and Iterator question
Linked List need help
string manipulation