3

In Java, I created a linked list class and added a bunch of values. But I don't know how to display those values.

I tried doing the following:

System.out.println(list);

But it printed out some weird values like ADTList@ followed by some jibberish.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user1307540
  • 31
  • 1
  • 2
  • 6
  • If you create your linked list by extending `AbstractSequentialList` you get the desired behaviour for free, as it's implemented by the grandparent `AbstractCollection`. – Christoffer Hammarström Apr 02 '12 at 11:30

8 Answers8

4
for(Object o : list){
    System.out.println(o);
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
dantuch
  • 9,123
  • 6
  • 45
  • 68
3

"ADTList@(followed by some jibberish)" is created by the default toString() method. Override this to print something useful. This may require overriding in both the list implementation and the elements.

Assuming that ADTList is your own implementation of a linked list, it's toString method could look something like this:

@Override
public String toString() {
    Iterator<Object> i = iterator();
    if (! i.hasNext()) {
        return "[]";
    }

    StringBuilder sb = new StringBuilder();
    sb.append('[');
    for (;;) {
        Object element = i.next();
        sb.append(e == this ? "(this Collection)" : element);
        if (! i.hasNext()) {
            return sb.append(']').toString();
        }
        sb.append(", ");
    }
}

This has been copied from the JDK's AbstractCollection class, as Christoffer Hammarström mentioned in his comment. It adds characters to make it list-like, e.g. "[x,y,z]" and also handles the list containing itself.

It might also be worth mentioning that the 'gibberish' in the default toString does actually have meaning. It is the name of the class the object is an instance of, and the hexadecimal value of object's hashCode - where the default hashCode(), will represent a memory location.

Grundlefleck
  • 124,925
  • 25
  • 94
  • 111
3

You will have to iterate through all the items in your linked list and print their values, or else, override the toString method for each of your items.

npinti
  • 51,780
  • 5
  • 72
  • 96
1

You have to get an Iterator on that list by invoking iterator(). Then call next() to get the elements until hasNext() returns false.

Iterator<Object> i = list.iterator();
while(i.hasNext())
    System.out.print(i.next()+"\t");
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
1

You need to iterate it to see the contents.

for(Object obj : list) {
  System.out.println(obj);
}

But your Object needs to have .toString() overriden.

Bogdan Emil Mariesan
  • 5,529
  • 2
  • 33
  • 57
1

You can print each value performing a for each loop on the list :

List<String> list = new LinkedList<>();
//Fill the list 

for (String s : list)
     System.out.println(s);
aleroot
  • 71,077
  • 30
  • 176
  • 213
1

If you have created your own implementation of a linked list (any reasons why you would do that?), you also need to override the default behavior of toString() if you want to print the content of the list in a user friendly way.

If you use the LinkedList provided in java.util, then the line of code you show in your question will print as expected.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • Thank You!! I added a toString method to both my Node and the ADTlist class and it just works! Thank you specifically and everyone else as well! – user1307540 Apr 03 '12 at 06:44
0

Override the toString method and write your own implementation to print useful Info About the same.

public String toString(){
   System.out.println("*** CONTENTS ***");
   for(Object obj: this){
     System.out.println(obj);
     //May be you will be required to override toString in your custom domain class to make it more useful
   }

   System.out.println("*** ******** ***");
}
Jayan
  • 18,003
  • 15
  • 89
  • 143
Akash Yadav
  • 2,411
  • 20
  • 32