• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

User-defined objects in collections

 
Ranch Hand
Posts: 57
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use like this

use getter and setter for accessing the private data member of a class.

then use foreach loop to display
for(Address adr:m1)
{
System.out.println(adr.getname());
System.out.println(adr.getstreet());
System.out.println(adr.getcity());
System.out.println(adr.getstate());
System.out.println(adr.getzipcode());
}
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You realise this is an 11‑year old thread? That technique would work well, however.
You should always use the code button. I thought of editing your post with the code button because the code looks much better, but you hadn't indented the code. We have some suggestions about indenting here. If you are quick, you can use the button to indent your code and add code tags for yourself.
 
reply
    Bookmark Topic Watch Topic
  • New Topic