• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using HashMap

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a small program here where I am using a HashMap object with a Map Reference and a Vector object with a List reference. The later works fine when passing it as a methods type to the main method.however the HashMap objects keys are output as Strings fine but the values which come from the constructors of another class do not .The program complies ok ,It is just the output which is confusing me , here`s the code:
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must just add the package and import statments are present in class a1 I must have missed them in the paste
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The version of System.out.println() that takes an Object as a parameter automatically calls the toString() method. The default implementation of toString() produces the output that you see here. For more details, see the javadocs for Object.toString(). If you wrote this class yourself, you should override the toString() method to print useful information about the object.

HTH

Layne
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi layne,
Thanks for the prompt reply as usual, just to cement my understanding of what is happening here , when you say System.out.println(foo); implements the Object classes toString method does this mean that when I create my own toString method in a2 that System...out(foo)automatically will implement that one.I have updated a2 with a toString() method and it does work :see below

Just another point on this , if my assumption is correct on the actions ive described , why does s2.getMap(); in the main method output the correct data if it does not have access to the toString method?

Thanks Danny

[ August 16, 2005: Message edited by: Daniel .j.Hyslop ]
[ August 16, 2005: Message edited by: Daniel .j.Hyslop ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic