在方法compareto(e)处找不到符号

nbnkbykc  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(615)

请帮助一个困惑的新手。我目前正在练习用add、remove、contains和tostring等方法实现二叉搜索树。我可以实现这个类来处理整数值,但是我不知道如何在二叉搜索树上使用泛型。我想灵活一些,也可以使用字符串作为我的二叉搜索树,或者我的card类。

public class MyTree<E> {

    private class Node implements Comparable<E> {
        private E data;
        private Node left;
        private Node right;
        public Node(E data) {
            this.data = data;
        }
        public int compareTo(E other) { 
            return this.data.compareTo(other); //ERROR HERE 
                                               //(Cannot Find Symbol at method CompareTo(E))
        }
    }

    private Node root;
    private int size;

    public int getSize() {
        return size;
    }

    public void add(E value) {
        this.root = add(value, root);
    }

    private Node add(E value, Node currentRoot) {
        if (currentRoot == null) {
            Node temp = new Node(value);
            size++;
            return temp;
        } else {
            if (currentRoot.compareTo(value) > 0)
                currentRoot.left = add(value, currentRoot.right);
            else if (currentRoot.compareTo(value) < 0)
                currentRoot.right = add(value, currentRoot.right);
            return currentRoot;
        }
    }

我有一个错误。

return this.data.compareTo(other);
                 ^
symbol: method compareTo(E)
location: variable data of type E
where E is a type-variable:
 E extends object declared in class MyTree

当我的节点类中有一个compareto(e other)时,为什么找不到compareto。我在node类中实现了类似的接口,但我不知道为什么。我也试过使用类似的工具,但也不起作用。

dfty9e19

dfty9e191#

问题是你已经及格了 E 作为参数 Comparable 但它没有定义。通常是实现 Comparable ,将自身作为参数传递给 Comparable (作为泛型参数)。 Comparable 应用于比较相同类型的示例。但是如果你看一下你的实现,你不是在比较这个类的两个示例,你只是在比较你的类的一个示例和这个文件 value 下一行中的另一个示例 add 方法:

if (currentRoot.compareTo(value) > 0)

你可以看到 currentRoot 属于类型 Node 值的类型 E . 这是一个错误的实现,因为如果你想有类型安全性,你不应该比较不同类型的值(或者如果你想比较它们,它们永远不应该相等,因为它们有不同的类型),那么为什么你不直接比较这两个呢 values 对彼此?
另一方面 E 是泛型类型,它不仅适用于 Node 班级, E 用于 add 你的方法 MyTree 上课也是。所以呢 E 应该是类的泛型参数 MyTree 你应该让 MyTree 由班级决定 E 当他想用的时候 MyTree 班级。另一个限制是 E 应该实施 Comparable 因为我们想比较 E 彼此之间。
最后你的课程将改为:

public class MyClassTree<E extends Comparable<E>> {
    private class Node {
        private E data;
        private Node left;
        private Node right;

        public Node(E data) {
            this.data = data;
        }
    }

    private Node root;
    private int size;

    public int getSize() {
        return size;
    }

    public void add(E value) {
        this.root = add(value, root);
    }

    private Node add(E value, Node currentRoot) {
        if (currentRoot == null) {
            Node temp = new Node(value);
            size++;
            return temp;
        } else {
            if (currentRoot.data.compareTo(value) > 0)
                currentRoot.left = add(value, currentRoot.right);
            else if (currentRoot.data.compareTo(value) < 0)
                currentRoot.right = add(value, currentRoot.right);
            return currentRoot;
        }
    }
}
1l5u6lss

1l5u6lss2#

您正在调用e类型中的compareto方法-该类型没有此方法。通过实现接口,类继承方法,而不是未知的类型参数类。含义:您必须在node类中创建自定义比较机制。
(注意:您还可以检查e是否实现了comparable,但是这更困难,并且在编译期间可能不可能做到。)
(注2:如果您感到困惑,请认为comparable意味着可以将实现类的示例与e类型对象进行比较。这甚至不是您想要的实现。)

相关问题