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 ??
Nodegeneric. Since it is an inner class, it will be generic due to the generic nature of the surrounding class.The type parameter E is hiding type E(compiler warning on line 4)