1

I am learning generics and want to create a generic linked list.

But i am getting following compile time error.

Type mismatch: cannot convert from LinkedList<E>.Node<E> to
 LinkedList<E>.Node<E>
public class LinkedList<E> {
    private Node<E> head = null;

    private class Node<E> {
        E value;
        Node<E> next;

        // Node constructor links the node as a new head
        Node(E value) {
            this.value = value;
            this.next = head;//Getting error here
            head = this;//Getting error here
        }
    }

    public void add(E e) {
        new Node<E>(e);
    }

    public void dump() {
        for (Node<E> n = head; n != null; n = n.next)
            System.out.print(n.value + " ");
    }

    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();
        list.add("world");
        list.add("Hello");
        list.dump();
    }
}

Please let me know why i am getting this error ??

2
  • You do not need to set Node generic. Since it is an inner class, it will be generic due to the generic nature of the surrounding class. Commented May 1, 2017 at 7:22
  • Setting the inner class generic type E is not working since The type parameter E is hiding type E (compiler warning on line 4) Commented May 1, 2017 at 7:24

1 Answer 1

8

The E here private class Node<E> { hides the E here :public class LinkedList<E> {

The Node class doesn't need to be generics. It contains a generics field value that depends on the E generics from LinkedList. It is enough.

public class LinkedList<E> {
    private Node head = null;

    private class Node {
        E value;
        Node next;

        // Node constructor links the node as a new head
        Node(E value) {
            this.value = value;
            this.next = head;//Getting error here
            head = this;//Getting error here
        }
    }

    public void add(E e) {
        new Node(e);
    }

    public void dump() {
        for (Node n = head; n != null; n = n.next)
            System.out.print(n.value + " ");
    }

    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();
        list.add("world");
        list.add("Hello");
        list.dump();
    }
}

EDIT

could you please tell me what compiler wants to say when it throw error message

When you write :

 this.next = head;

you have to be aware that these two variables don't rely on the same type.

  • next is a field declared in Node<E> class in this way : Node<E> next

  • head is a field declared in LinkedList<E> class in this way : Node<E> head

But the E type declared in the Node<E> class is not considered by the compiler as being the same E type declared in the LinkedList<E> class because these are two distinct type declarations.

So here :

this.next = head;

the compiler cannot assign from LinkedList<E>.Node<E> to LinkedList<E>.Node<E> because the Node<E> next field from the Node<E>class and the Node<E> head field from the LinkedList<E>class don't declare the same type (and are not convertible either).

Sign up to request clarification or add additional context in comments.

1 Comment

could you please tell me what compiler wants to say when it throw error message

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.