Java的compareTo方法 - 找不到符号错误

时间:2014-11-15 02:27:00

标签: java

Java noob在这里试图实现一个AVL树但是我正在试图让compareTo()方法起作用。对不起代码转储!

public class AVLTree<AnyType extends Comparable<? super AnyType>>
{

    //Constructor
    AVLTree()
    {
        root = null;
    }

    public void SanityCheck()
    {
        System.out.println("Hello World");
    }

    public void Insert(AnyType x, AVLNode<AnyType> insertAtNode)
    {

        //if starting node is null, it is the root.
        if(insertAtNode == null){
            if(root == null){
                root = new AVLNode(x);
            } else{
                Insert(x, root);
            }
        } else{
            if(compareTo(insertAtNode.element, x) > 0){ //Bias here.  Fix later
                if(insertAtNode.rightChild == null){
                    AVLNode newNode = new AVLNode(x);
                    insertAtNode.rightChild = newNode; //Actual insertion happens here
                } else {
                    Insert(x, insertAtNode.rightChild);
                }
            } else{
                if(insertAtNode.leftChild == null){
                    AVLNode newNode = new AVLNode(x);
                    insertAtNode.leftChild = newNode; //Actual insertion happens here
                } else{
                    Insert(x, insertAtNode.leftChild);
                }
            }
        }
    }

    private class AVLNode<AnyType extends Comparable<? super AnyType>>
    {
    //Constructor
        AVLNode(AnyType theElement)
        {
            element = theElement;
        }

        AnyType element;
        AVLNode<AnyType> leftChild;
        AVLNode<AnyType> rightChild;
    }

    AVLNode root;
}

忽略算法 - 它显然未完成。但是,当我尝试编译时,为什么还要继续获取“找不到符号:compareTo”?感谢

1 个答案:

答案 0 :(得分:1)

您当前的课程中没有名为compareTo的方法。

您可能打算调用compareTo的{​​{1}}方法:

insertAtNode.element

另外需要注意的是,通过Java约定,方法名称是camelCase。例如,if (insertAtNode.element.compareTo(x) > 0) { ... } 应为SanityCheck