• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 24204
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: 76888
366
  • 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: 76888
366
  • 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: 22702
129
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
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic