我正在尝试在我的班级中使用compareTo(),但它一直给我这个错误:
The method compareTo(T) in the type Comparable<T> is not applicable for the arguments (Object)
虽然简单的转换可以修复此错误并且代码运行正常但我现在想以正确的方式执行此操作
public class BinarySearchTreeImpl<T extends Comparable<T>> implements BinarySearchTree<T>, Iterable{
private Node root;
@Override
public boolean isEmpty() {
return this.root == null;
}
@Override
public boolean insert(T item) throws DuplicateItemException {
Node newNode = new Node(item);
if(isEmpty()){
this.root = newNode;
return true;
}
else{
Node current = this.root;
while(current != null){
if(item.compareTo(current.item) == -1 ){ // The method compareTo(T) in the type Comparable<T> is not applicable for the arguments (Object)
.
.
.
public class Node<T>{
T item;
Node leftChild;
Node rightChild;
public Node(T item){
this.item = item;
}
.
.
.
如果我将Node类更改为Node<T extends Comparable<T>
,我会得到几乎相同的错误
The method compareTo(T) in the type Comparable<T> is not applicable for the arguments (Comparable)