• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to print objects from linked list?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a linkedlist with different objects in it's nodes. I don't know how can i print objects of it??
my code is: i don't know what i write in the body of dispalyData method? in this linked list we have objects of manager class , laborer class ... that each of them has special fields . but all of them have name, last name and idnumber fields.


LinkedList<Employee> list=new LinkedList<Employee>();

public void displayData(){

for (Employee e:list){
.....

}
}

I'm waiting for your help
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sheila Jorge wrote:I have a linkedlist with different objects in it's nodes. I don't know how can i print objects of it??
...
I'm waiting for your help


Again, it depends what you mean by "print".
All classes have a toString() method that you can override, and this is normally used to provide some sort of text-based representation of the instance.

I suspect (at least to start with) that's all you'll need; if you need anything more involved, you might consider a Printable interface (maybe with a print() method), and have all your list classes implement it. That way, your list can simply be a List<Printable>.

Winston
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The toString() method is overridden in the following manner in abstract collection.


Bonus: How to print array elements using one line?



This is another disadvantage of using arrays. We need the help of Arrays.toString method to print the array else we'll get hexadecimal representation of the hashcode of the array object.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic