简体   繁体   English

未定义 Comparable 类型的 compareTo(Comparable) 方法

[英]The method compareTo(Comparable) is undefined for the type Comparable

So i am building a library as project that covers not all but most of the data structures and i found myself with this problem:因此,我正在构建一个库作为项目,该项目不仅涵盖了所有数据结构,而且涵盖了大部分数据结构,但我发现自己遇到了这个问题:

package structure.tree;

import structure.Structure;
import structure.node.BinaryNodeKeyValue;

public class AVLTree<Comparable,V> extends Tree<BinaryNodeKeyValue<Comparable,V>> {
    
    private static final long serialVersionUID = 5046115177325966348L;

    public AVLTree(){

    }

    @Override
    public int compareTo(Structure<BinaryNodeKeyValue<Comparable,V>> o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean containsValue(BinaryNodeKeyValue<Comparable,V> elem) {
        return containsValueAux(((BinaryNodeKeyValue<Comparable,V>) super.getValue()), elem);
    }

    private boolean containsValueAux(BinaryNodeKeyValue<Comparable,V> root, BinaryNodeKeyValue<Comparable,V> elem){
        if(root == null) return false;
        else {
            if(root.equals(elem)) return true;
            else return containsValueAux(root.getLeft(), elem) || containsValueAux(root.getRight(), elem);
        }
    }

    public boolean containsKey(Comparable key){
        return containsKeyAux(((BinaryNodeKeyValue<Comparable,V>) super.getValue()), key);
    }

    private boolean containsKeyAux(BinaryNodeKeyValue<Comparable,V> root, Comparable key){
        if(root == null) return false;
        else {
            if(root.getKey().compareTo(key) > 0) return containsKeyAux(root.getRight(), key);
            else if(root.getKey().compareTo(key) < 0) return containsKeyAux(root.getLeft(), key);
            else return true;
        }
    }

    @Override
    public void deleteValue(BinaryNodeKeyValue<Comparable,V> elem) {
        // TODO Auto-generated method stub

    }
    
    @Override
    public void insertValue(BinaryNodeKeyValue<Comparable,V> elem) {
        // TODO Auto-generated method stub

    }

    @Override
    public BinaryNodeKeyValue<Comparable,V>[] toArray() {
        // TODO Auto-generated method stub
        return null;
    }

    public BinaryNodeKeyValue<Comparable,V> get(BinaryNodeKeyValue<Comparable,V> root, Comparable key){
        return getAux(root, key);
    }

    private BinaryNodeKeyValue<Comparable, V> getAux(BinaryNodeKeyValue<Comparable, V> root, Comparable key) {
        return null;
    }

}

At rows 40 and 41 (rows 3 and 4 of method containsKeyAux) it says "The method compareTo(Comparable) is undefined for the type Comparable" and this blows my mind cause the method compareTo is actually defined inside Comparable interface only.在第 40 行和第 41 行(方法 containsKeyAux 的第 3 行和第 4 行)它说“Comparable 类型的方法 compareTo(Comparable) 是未定义的”,这让我大吃一惊,因为方法 compareTo 实际上只在 Comparable 接口中定义。 VS Code is also showing me a warning at row 6 that says "The type parameter Comparable is hiding the type Comparable" but i am trying to make the Comparable type as generic as possible cause the key of nodes could be a String, Integer, or a different type of Object. VS Code 还在第 6 行向我显示了一条警告,上面写着“类型参数 Comparable 隐藏了 Comparable 类型”,但我试图使 Comparable 类型尽可能通用,因为节点的键可能是字符串、Integer 或不同类型的 Object。

When you declare a generic like this AVLTree<Comparable,V> you have created a class with two generic types Comparable and V and Comparable has nothing to do with the interface Comparable , they just happen to have the same name.当您声明像这样的AVLTree<Comparable,V>的泛型时,您创建了一个具有两个泛型类型 Comparable 和 V 的 class ,而 Comparable 与接口Comparable无关,它们恰好具有相同的名称。

You probably meant你可能是说

class AVLTree<T extends Comparable, V>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM