File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using toString()

 
Gwen Smith
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please paste declaration of theList object?
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gwen Smith
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
theList is an array of objects (Contact[])

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your list() method doesn't do anything.
 
Rob Spoor
Sheriff
Pie
Posts: 20398
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic