为什么compareTo(T)将其参数作为我传递的T类的对象?

时间:2018-03-20 21:23:46

标签: comparable compareto

我正在尝试在我的班级中使用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)

0 个答案:

没有答案