aspose file tools*
The moose likes Java in General and the fly likes how to print objects from linked list? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to print objects from linked list?" Watch "how to print objects from linked list?" New topic
Author

how to print objects from linked list?

Sheila Jorge
Greenhorn

Joined: Oct 24, 2011
Posts: 16
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

Joined: Mar 17, 2011
Posts: 7779
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287


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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to print objects from linked list?