我正在写一个 sortedAdd(T node)
LinkedList
中的方法.我如何比较node.key_
与 current.key_
?我不能使用 <
(升序),因为它是通用的。
public void sortedAdd(T node){
...
Node<T> current = header;
while (current != null) {
if (node.key_ < current.key_) {
...
}
}
}
最佳答案
java.lang.Object不实现 Comparable。 Collections.sort(List)期望 T
实现 Comparable。
Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface.
我相信你的代码应该是:
List<String> list = new ArrayList<String>();
关于java - Java 中的 Collections.sort(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15084671/