4

I have written a small piece of code to implement a linked list data structure. I have an internal class "Node" that has two fields Node and value. Constructor of Linked list accept int value parameter and assign that value to the Node object and add the Node object to the LinkedList object.

My question is which code of java.util.LinkedList makes the list object to be printed as a list of number but not the address of its object?

As When i print "list1", the output is [3,4]. When I print "list", the output is hashcode of the object address.

I didn't find the toString() in java.util.LinkedList class.

How can I make my code to print the content of LinkedList?

Below is the code:

class LinkedList {

    Node first;

    Node getNode(){
        return new Node();
    }

    class Node{
        Node next;
        int value;
    }

    void add(int value){
        Node n=this.getNode();
        n.value=value;
        n.next=null;

        if (first==null){
            first=n;
        } else{
            first.next=n;
        }
    } 
}

public class LinkedListTest{
    public static void main(String[] args) {
        LinkedList list=new LinkedList();
        java.util.LinkedList<Integer> list1=new java.util.LinkedList<>();
        list1.add(3);
        list1.add(4);
        list.add(1);
        list.add(2);
        System.out.println(list);
        System.out.println(list1);
    }
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
akshit bhatia
  • 573
  • 6
  • 22
  • The `LinkedList` class needs a `toString()` function. The class of the objects you are adding also needs one. In this case, you are adding `Integer` objects which already has `toString()`. – Code-Apprentice Jun 23 '18 at 21:04

2 Answers2

3

You have to create your own toString method for example

class LinkedList {

    //...
    @Override
    public String toString() {
        StringBuilder text = new StringBuilder("[");
        String del = "";
        if (first != null) {
            do {
                text.append(del).append(first.value);
                first = first.next;
                del = ", ";
            } while (first != null);
        }
        text.append(']');
        return text.toString();
    }
}

If you run your code again, the Outputs

[1, 2]
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
3

Your class LinkedList (I suggest you rename it since it might be confused with java.util.LinkedList) needs to override the method Object::toString, which is called within printing out to a console.

I didn't find the toString() in java.util.LinkedList class.

A bit detective job - you have to click through the source codes of LinkedList<E> which extends AbstractSequentialList<E> which extends AbstractList<E> which finally extends AbstractCollection<E> (source code) class where is overridden Object::toString method responsible for the String-alike representation of all the element. There you can get inspired.

How can I make my code to print the content of LinkedList?

This way:

@Override
public String toString() {
    StringBuilder sb = new StringBuilder("[");
    if (first != null) {
        Node temp = first;
        String sep = "";
        while (temp != null) {
            sb.append(sep).append(temp.value);
            temp = temp.next;
            sep = ", ";
        }
    }
    return sb.append(']').toString();
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183