File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes using toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using toString()" Watch "using toString()" New topic
Author

using toString()

Gwen Smith
Greenhorn

Joined: Jan 23, 2011
Posts: 5
I'm trying to print out an array (theList) of objects, and I keep getting something like this: [LContact;@1389e4

I have a toString() method:


and the bit that should print it out
I think it's the second part that's wrong, but I'm not sure why, or how to make it right.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

can you please paste declaration of theList object?


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I think it's the second part that's wrong, but I'm not sure why, or how to make it right.

Calling toString on your list (what type is it ? ArrayList ?) will not iterate through it and call the toString method of your list items. You have to loop through the list yourself.

List classes like ArrayList do not override the toString() method, that's why you see the result of the default implementation.


[My Blog]
All roads lead to JavaRanch
Gwen Smith
Greenhorn

Joined: Jan 23, 2011
Posts: 5
theList is an array of objects (Contact[])

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Christophe Verré wrote:
List classes like ArrayList do not override the toString() method


They do, actually; it's arrays that don't do this, and Gwen has an array.

Gwen, therefore, to do a cheap and easy display of the array's contents, you might convert your array to an ArrayList, something like

System.out.println(new ArrayList(Arrays.asList(theList)));

but to do anything nicer/more customized, you have to use a for-loop to visit each element of the array and print it yourself.


[Jess in Action][AskingGoodQuestions]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

theList is... an array Well, what I said previously still stands. You have to loop through the array.
And by the way, you don't need to explicitly call toString. System.out.println takes care of that.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Ernest Friedman-Hill wrote:
Christophe Verré wrote:
List classes like ArrayList do not override the toString() method


They do, actually; it's arrays that don't do this, and Gwen has an array.

My minute of fame
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

If you want to go with the array- You can have a look at- Arrays.toString() method in java.util.Arrays class. But again your Contact class should override toString() to return a meaningful output.


Mohamed Sanaulla | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
This section in the Java™ Tutorials (and the "arrays" section) should give hints about how to print the individual elements in your array.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Your list() method doesn't do anything.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Ernest Friedman-Hill wrote:Gwen, therefore, to do a cheap and easy display of the array's contents, you might convert your array to an ArrayList, something like

System.out.println(new ArrayList(Arrays.asList(theList)));

but to do anything nicer/more customized, you have to use a for-loop to visit each element of the array and print it yourself.

I guess you haven't discovered the Arrays.toString methods yet, that Mohamed already mentioned


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using toString()
 
Similar Threads
What's the difference in this case? (so basic)
printing out all my instances...toString() method..
toString
how do I print the elements in the Arraylist without the null.
Formatting doubles in a toString return