java compareTo减法值
我正在使用java compareTo减法值,java,comparable,compareto,Java,Comparable,Compareto,我正在使用Binarytree和 removeNode(E value) method (E extends Comparable<E>), 编辑: 然而,当我尝试 System.out.println(value.compareTo(root.getData()); 我得到value减去root.getData()的值 这正是compareTo应该返回的结果: 返回:如下所示的负整数、零或正整数 对象小于、等于或大于指定对象 参见这是Short根据文档所做的: 如果此Shor
Binarytree
和
removeNode(E value) method (E extends Comparable<E>),
编辑:
然而,当我尝试
System.out.println(value.compareTo(root.getData());
我得到value减去root.getData()的值 这正是
compareTo
应该返回的结果:
返回:如下所示的负整数、零或正整数
对象小于、等于或大于指定对象
参见这是Short根据文档所做的: 如果此Short等于参数Short,则值为0;如果短参数的数值小于短参数,则值小于0;如果短参数在数值上大于短参数(有符号比较),则该值大于0 因此,如果要比较相等的短路,则应返回0:
Short s = new Short("12");
Short anotherShort = new Short("12");
if(s.compareTo(anotherShort) == 0){
System.out.println("Equal");
}
else {
System.out.println("Not Equal");
}
应打印“不相等”
更明确的示例如下:
这张照片是:
50 is less than 200, difference = -150
50 is equal to 50, difference = 0
200 is more than 50, difference = 150
我们不能调试不可见的代码。在这里发布相关部分。ehm。。。什么?compareTo总是返回一个int。这有什么问题吗?你认为compareTo应该返回什么?编辑。我想它会返回1,0或-1。但是我得到了区别,这是一种可能的实现方法,但它实际上返回一个正数(不是零)、一个负数或零
System.out.println(value.compareTo(root.getData());
Short s = new Short("12");
Short anotherShort = new Short("12");
if(s.compareTo(anotherShort) == 0){
System.out.println("Equal");
}
else {
System.out.println("Not Equal");
}
package com.tutorialspoint;
import java.lang.*;
public class ShortDemo {
public static void main(String[] args) {
// create short object and assign value to it
short val1 = 50, val2 = 200, val3 = 50;
Short Shortval1 = new Short(val1);
Short Shortval2 = new Short(val2);
Short Shortval3 = new Short(val3);
// returns less than 0 if this Short is less than the argument Short
int cmp = Shortval1.compareTo(Shortval2);
System.out.println("" + Shortval1 + " is less than " + Shortval2 + ", difference = " + cmp);
// returns 0 if this Short is equal to the argument Short
cmp = Shortval1.compareTo(Shortval3);
System.out.println("" + Shortval1 + " is equal to " + Shortval3 + ",difference = " + cmp);
// returns greater than if this Short is greater than the argument Short
cmp = Shortval2.compareTo(Shortval1);
System.out.println("" + Shortval2 + " is more than " + Shortval1 + ",
difference = " + cmp);
}
}
50 is less than 200, difference = -150
50 is equal to 50, difference = 0
200 is more than 50, difference = 150