我正在用 Java 编写一个通用的链表实现。代码是
public class LinkedList<T> {
private Node<T> top;
private int no_of_items;
private class Node<T extends Comparable<? super T>> {
T data;
Node<T> next;
}
public LinkedList() {
top = null;
}
public boolean isEmpty() {
return (top == null);
}
public void insert(T item) {
Node<T> node = new Node<T>();
node.data = item;
node.next = top;
if(isEmpty()) {
top = node;
} else {
Node<T> n = top;
while(n.next != null) {
n = n.next;
}
n.next = node;
}
no_of_items++;
}
}
我想要的是T
应该是Comparable
。在编译此代码时,我在初始化 Node.js 时遇到错误。
Bound Mismatch the type T is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> of the type LinkedList<T>.Node<T>
我无法弄清楚这里有什么问题。