Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

using toString()

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please paste declaration of theList object?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
theList is an array of objects (Contact[])

 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your list() method doesn't do anything.
 
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic