第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法比較的可比較類型(對象)

無法比較的可比較類型(對象)

繁星點點滴滴 2021-08-25 16:40:20
我收到這個神秘的錯誤:運算符 > 未定義參數(shù)類型 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);            }        }    }}
查看完整描述

1 回答

?
一只萌萌小番薯

TA貢獻1795條經(jīng)驗 獲得超7個贊

Java 沒有運算符重載。您不能將 Comparable 類型與>. 你需要root.val.compareTo(newNode.val)改用。

作為旁白:

  • Comparable 是一個接口,而不是一個類

  • 你不需要指定 <P extends Comparable<P>>

  • addValHelper代碼移動到 Node 類本身可能更有意義

  • 它可能是有意義的Node實現(xiàn)Comparable

這樣,您的代碼感覺更加地道,并且您不會將 Node 的字段暴露給 BST。

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

    private final Node<T> root;


    /** Presumably this is run when a value is added.. */

    private void addValueHelper(Node rootNode, Node newNode) {

        rootNode.attachChild(newNode);

    }


    public static class Node implements Comparable<T> {

        private final T val;

        private Node left;

        private Node right;


        public Node(T val) {

            this.val = val;

        }


        public int compareTo(Node other) {

            return this.val.compareTo(other.val);

        }


        /**

         * Takes the given node and compares it with the current node.

         * If the current node is greater than the given node, the given node is placed to the left.

         * Otherwise it is placed to the right.

         */

        protected void attachChild(Node newNode) {

            if (this.compareTo(newNode) == 1) {

                if (this.left == null) {

                    this.left = newNode;

                    return;

                }

                this.left.attachChild(newNode);

                return;

            } 


            if (this.right == null) {

                this.right = newNode;

                return;

            }


            this.right.attachChild(newNode);

        }

    }

}


查看完整回答
反對 回復(fù) 2021-08-25
  • 1 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號