Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting object data back from an arraylist

 
kel dobson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
trying to get output to screen terminal from an arraylist of objects using system.out.print(ln)
the terminal window prints out the membory reference used, either by java (or BlueJ), but not the fields of the object I want to retrieve
apologies for my ignorance but any advice greatly appreciated
as you will all quickly guess - I am very new to Java!!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kel,
Welcome to JavaRanch!
If you ask Java to "print" an Object, either by calling System.out.println() on it, or by calling System.out.println() on an ArrayList containing it, what happens is that println() calls the toString() method on that object and then displays the returned String. The default implementation of toString() returns what you're calling the "memory reference" -- it's actually composed of the hash code for the object and the name of the class. If you want the object to print in some other way, you just have to implement your own version of toString() in your class -- i.e.,
 
kel dobson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for your speedy and helpful response. Thought I understood this object reference stuff but I am still confused.
Hope you don't mind but I will include my code - hope I can get the hang of the code section here!?
A further class instantiates instances of "Members" but I have populated the ArrayList with 4 examples for testing.
Similar code runs in a different project without trouble and I thought I had carefully transposed the variables and object references to the new class - but it returns the 'Class and Hash code' as you mention.
I am even more confused as the 'get method' - "numberOfMembers" correctly returns the int '4' so I think I am pointing the code to the correect class etc?
Here's hoping you can point me in the right direction?

[ December 20, 2003: Message edited by: kel dobson ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This part is fine, but it doesn't get at the heart of the problem, which is, "Did you define a toString() method in the Member class?"
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, what happens if you try
System.out.println( new Member("Fred", 21, "D30") );
??

If the Member class does not have a toString() method in it, you'll get something that looks like garbage.
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want to override the toString() method for the Member class, you can print individual member variables of the Member class (provided, of course, that the Member class does expose).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WorldCitizen --
Welcome to JavaRanch!
We don't have too many rules around these parts but we do have our naming policy, which requires that you have a real-sounding name containing first and last components. Most people here use their real name -- it keeps us honest. Please head over here and change yours, pronto. Thanks, pardner!
 
kel dobson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Is it OK to continue on this theme please?
sorry, but I don't understand what you mean by " . . . member does expose"??
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kel,
You still haven't answered Marilyn's or Ernest's questions above.
Perhaps the following demonstration will clear up the concept.
The above example printed Foo@7259da on my system.The above code example printed this Foo's a is 5 on my system.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic