php editor Yuzi brings you a selection of java Q&A. Today’s question: Type mismatch: Unable to convert from DLL
I am trying to implement a doubly linked list with nested node classes. Due to the generic nature of the class I'm getting a type mismatch error. Since the nested class is not a static class, I thought it would use the generic type operator from the top class.
public class DLL <E> { public class Node<E>{ private E element; private Node<E> prev; private Node<E> next; public <E> Node (E element, Node <E> prev, Node<E> next){ this.element = element; // Error: Type mismatch: cannot convert from E to E this.prev = prev; // Error: Type mismatch: cannot convert from DLL<E>.Node<E> to DLL<E>.Node<E> this.next = next;// Error: Type mismatch: cannot convert from DLL<E>.Node<E> to DLL<E>.Node<E> }
Any help would be great!
Instead of redeclaring the generic type e
. The node constructor should use the generic type e
from the external dll<e>
class. The solution is as follows:
public class DLL<E> { public class Node { private E element; private Node prev; private Node next; public Node(E element, Node prev, Node next) { this.element = element; this.prev = prev; this.next = next; } } }
The above is the detailed content of Type mismatch: cannot convert from DLL