Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在将BST中节点的键与0进行比较时,为什么不能使用.compareTo()? 这并不是类中包含的所有代码,但如果这还不够,我还将添加其余的代码 add()用于使用键将值添加到BST中的正确位置。如果密钥已存在,则不执行任何操作_Java_Binary Search Tree_Compareto - Fatal编程技术网

Java 在将BST中节点的键与0进行比较时,为什么不能使用.compareTo()? 这并不是类中包含的所有代码,但如果这还不够,我还将添加其余的代码 add()用于使用键将值添加到BST中的正确位置。如果密钥已存在,则不执行任何操作

Java 在将BST中节点的键与0进行比较时,为什么不能使用.compareTo()? 这并不是类中包含的所有代码,但如果这还不够,我还将添加其余的代码 add()用于使用键将值添加到BST中的正确位置。如果密钥已存在,则不执行任何操作,java,binary-search-tree,compareto,Java,Binary Search Tree,Compareto,如果指定的键位于树中,则contains()应返回True ```public class Node { public Node left; public Node right; public int key; public String value; public void add ( int key, String value ) { if ( key.compareTo ( this.key ) < 0)

如果指定的键位于树中,则contains()应返回True

```public class Node
{
    public Node left;
    public Node right;
    public int key;
    public String value;

    public void add ( int key, String value )
    {
        if ( key.compareTo ( this.key ) < 0)
        {
            if ( left != null )
                left.add ( key, value )
            else
                left = new Node ( key, value );
        }
        else if ( key.compareTo ( this.key ) > 0 )
        {
            if ( right != null )
                right.add ( key, value );
            else
                right = new Node ( key, value);
        }
        else
            this.value = value;
    }

    public boolean contains ( int key )
    {
        if ( this.key == ( key ) )
            return value;
        if ( key.compareTo ( this.key ) < 0 )
            return left == null ? null : left.contains ( key );
        else
            return right == null ? null : right.contains ( key );
    }
}

问题在于,
int
是基本变量,因此不实现可比性,因此不能使用int.compareTo,而装箱变量整数可以。您可以简单地使用Integer而不是int,或者使用Integer。比较(1,2)并保留原语的用法

public static class Node {
    public Node left;
    public Node right;
    public Integer key;
    public String value;

    public Node(Integer key, String value) {
        this.key = key;
        this.value = value;
    }

    public void add(Integer key, String value) {
        if (key.compareTo(this.key) < 0) {
            if (left != null)
                left.add(key, value);
            else
                left = new Node(key, value);
        } else if (key.compareTo(this.key) > 0) {
            if (right != null)
                right.add(key, value);
            else
                right = new Node(key, value);
        } else
            this.value = value;
    }

    public boolean contains(Integer key) {
        if (this.key.intValue() == (key)) {
            return true;
        }
        if (key.compareTo(this.key) < 0)
            return left == null ? null : left.contains(key);
        else
            return right == null ? null : right.contains(key);
    }
}
公共静态类节点{
公共节点左;
公共节点权;
公开整数密钥;
公共字符串值;
公共节点(整数键、字符串值){
this.key=key;
这个值=值;
}
公共void add(整型键、字符串值){
if(key.compareTo(this.key)<0){
if(左!=null)
左。添加(键、值);
其他的
左=新节点(键、值);
}如果(key.compareTo(this.key)>0,则为else{
if(右!=null)
右。添加(键、值);
其他的
右=新节点(键、值);
}否则
这个值=值;
}
公共布尔包含(整数键){
if(this.key.intValue()==(key)){
返回true;
}
if(key.compareTo(this.key)<0)
返回left==null?null:left.contains(键);
其他的
return right==null?null:right.contains(key);
}
}

问题在于
int
是原始变量,因此不实现可比较,因此不能使用int.compareTo,而装箱变量整数可以。您可以简单地使用Integer而不是int,或者使用Integer。比较(1,2)并保留原语的用法

public static class Node {
    public Node left;
    public Node right;
    public Integer key;
    public String value;

    public Node(Integer key, String value) {
        this.key = key;
        this.value = value;
    }

    public void add(Integer key, String value) {
        if (key.compareTo(this.key) < 0) {
            if (left != null)
                left.add(key, value);
            else
                left = new Node(key, value);
        } else if (key.compareTo(this.key) > 0) {
            if (right != null)
                right.add(key, value);
            else
                right = new Node(key, value);
        } else
            this.value = value;
    }

    public boolean contains(Integer key) {
        if (this.key.intValue() == (key)) {
            return true;
        }
        if (key.compareTo(this.key) < 0)
            return left == null ? null : left.contains(key);
        else
            return right == null ? null : right.contains(key);
    }
}
公共静态类节点{
公共节点左;
公共节点权;
公开整数密钥;
公共字符串值;
公共节点(整数键、字符串值){
this.key=key;
这个值=值;
}
公共void add(整型键、字符串值){
if(key.compareTo(this.key)<0){
if(左!=null)
左。添加(键、值);
其他的
左=新节点(键、值);
}如果(key.compareTo(this.key)>0,则为else{
if(右!=null)
右。添加(键、值);
其他的
右=新节点(键、值);
}否则
这个值=值;
}
公共布尔包含(整数键){
if(this.key.intValue()==(key)){
返回true;
}
if(key.compareTo(this.key)<0)
返回left==null?null:left.contains(键);
其他的
return right==null?null:right.contains(key);
}
}

要显示周围的代码并包括
键的类型。@Jason我编辑了这篇文章,以包括其中包含的大部分类。您知道不能用这种方式使用compareTo()吗?你大概收到了一条错误消息?该错误消息应该指向告诉您为什么不能的方向。在这种情况下,你应该在问题中包含错误信息,你从研究错误信息中学到了什么,还有什么还不清楚。当然,还有一些错误消息不清楚!但至少要包含它们,这样我们就不必重做编译器已经为我们做过的诊断工作。要显示周围的代码,并包含
key
的类型吗。@Jason我编辑了这篇文章,将其中包含的大多数类都包含在内。您知道不能以这种方式使用compareTo()吗?你大概收到了一条错误消息?该错误消息应该指向告诉您为什么不能的方向。在这种情况下,你应该在问题中包含错误信息,你从研究错误信息中学到了什么,还有什么还不清楚。当然,还有一些错误消息不清楚!但至少要包含它们,这样我们就不必重复编译器已经为我们完成的诊断工作。