无法比较的可比较类型(对象)

我收到这个神秘的错误:


运算符 > 未定义参数类型 java.lang.Comparable, java.lang.Comparable


有没有搞错?


(这是代码)


public class BST<T extends Comparable<T>> {

    public static class Node<P extends Comparable<P>> {

        P val;

        Node<P> left;

        Node<P> right;


        public Node() {


        }


        public Node(P val) {

            this.val = val;

        }

    }


    Node<T> root;


    private void addValHelper(Node root, Node newNode) {

        if (root.val > newNode.val) { // <-- ERROR IS HERE

            if (root.left == null) {

                root.left = newNode;

            } else {

                addValHelper(root.left, newNode);

            }

        } else {

            if (root.right == null) {

                root.right = newNode;

            } else {

                addValHelper(root.right, newNode);

            }

        }

    }

}


繁星点点滴滴
浏览 136回答 1
1回答

一只萌萌小番薯

Java 没有运算符重载。您不能将 Comparable 类型与>.&nbsp;你需要root.val.compareTo(newNode.val)改用。作为旁白:Comparable 是一个接口,而不是一个类你不需要指定&nbsp;<P extends Comparable<P>>将addValHelper代码移动到 Node 类本身可能更有意义它可能是有意义的Node实现Comparable。这样,您的代码感觉更加地道,并且您不会将 Node 的字段暴露给 BST。public class BST<T implements Comparable<T>> {&nbsp; &nbsp; private final Node<T> root;&nbsp; &nbsp; /** Presumably this is run when a value is added.. */&nbsp; &nbsp; private void addValueHelper(Node rootNode, Node newNode) {&nbsp; &nbsp; &nbsp; &nbsp; rootNode.attachChild(newNode);&nbsp; &nbsp; }&nbsp; &nbsp; public static class Node implements Comparable<T> {&nbsp; &nbsp; &nbsp; &nbsp; private final T val;&nbsp; &nbsp; &nbsp; &nbsp; private Node left;&nbsp; &nbsp; &nbsp; &nbsp; private Node right;&nbsp; &nbsp; &nbsp; &nbsp; public Node(T val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.val = val;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public int compareTo(Node other) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.val.compareTo(other.val);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Takes the given node and compares it with the current node.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* If the current node is greater than the given node, the given node is placed to the left.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Otherwise it is placed to the right.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; protected void attachChild(Node newNode) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.compareTo(newNode) == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.left == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.left = newNode;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.left.attachChild(newNode);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.right == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.right = newNode;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.right.attachChild(newNode);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java