• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to print objects from linked list?

 
Sheila Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
 
Winston Gutkowski
Bartender
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic