wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes getting object data back from an arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting object data back from an arraylist" Watch "getting object data back from an arraylist" New topic
Author

getting object data back from an arraylist

kel dobson
Greenhorn

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.,


[Jess in Action][AskingGoodQuestions]
kel dobson
Greenhorn

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jul 22, 2000
Posts: 9044
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Sadanand Murthy
Ranch Hand

Joined: Nov 26, 2003
Posts: 382
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).


Ever Existing, Ever Conscious, Ever-new Bliss
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting object data back from an arraylist