具有双值的 Java CompareTO

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22405015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:33:14  来源:igfitidea点击:

Java CompareTO with double Values

java

提问by Softey

I need to be able to rearrange a group of people from their overall fitness score. I am able to do a compareTO method with ints but as soon as its with doubles I get errors flying up everywhere.

我需要能够根据他们的整体健康得分重新安排一群人。我可以用 ints 做一个 compareTO 方法,但是一旦它用双打,我就会到处都是错误。

public double compareTo(FitnessScore o){
    return (this.overall-o.overall);
}

It needs to be a double because there are scores like:

它需要是双倍的,因为有如下分数:

68.5 68.4 60.1 60.3

So casting them to an int would make it redundant. When I did have it as an int to see if it would work. I tried the following. (subjects being the array that I initialized to hold all the different instances of people)

因此,将它们转换为 int 会使其变得多余。当我确实将其作为 int 以查看它是否有效时。我尝试了以下方法。(主题是我初始化的数组以保存所有不同的人实例)

Arrays.sort(subjects);

I get the following error:

我收到以下错误:

java.lang.NullPointerExceptione

Any suggestions?

有什么建议?

采纳答案by Aditya

Your compareTomust alaways return an intvalue.

compareTo必须始终返回一个int值。

-1for placing it before in Collection
0indicates same value already exists in Collection
+1for placing it afterwards in Collection

-1将它放在 Collection
0之前表示Collection 中已经存在相同的值
+1将它放在 Collection 之后

If overallis of type Double, use this:

如果overall是 type Double,请使用:

public int compareTo(FitnessScore o){
    return this.overall.compareTo(o.overall));
}

If overallis of type double, use this:

如果overall是 type double,请使用:

public int compareTo(FitnessScore o){
    if(this.overall<o.overall)
          return -1;
    else if(o.overall<this.overall)
          return 1;
    return 0;
}

And for your NullPointerException, check that your subjectsis not null.

对于您的NullPointerException,请检查您subjects是否为空。

回答by GreenOnBlack

You need to implement the Comparator interface. Dont worry about your values. Even if the compare method is returning an int. This int is not one of your values.

您需要实现 Comparator 接口。不要担心你的价值观。即使比较方法返回一个整数。这个 int 不是你的价值观之一。

Also check this out. Comparator with double type

也检查一下。 双型比较器

回答by Mauren

Your compareTo()method receives a FitnessScoreobject. However, it does not test for null case.

您的compareTo()方法接收一个FitnessScore对象。但是,它不会测试空情况。

When ois null, your method will throw a NullPointerException.

o是时null,您的方法将抛出一个NullPointerException.

回答by Arjit

You can try this.

你可以试试这个。

Double obj1 = this.overall;
Double obj2 = o.overall;

int retval =  obj1.compareTo(obj2);

//then do your normal if else block.

//然后执行正常的 if else 块。

回答by Boris

You should implement a Comparatorinterface:

你应该实现一个Comparator接口:

class OFSComparator implements Comparator<Member> {
   public int compare(Member m1, Member m2) {
      return (m1.ofs.compareTo(m2.ofs));
   }
}

Each Member should have a field for overall fitness score

每个成员都应该有一个整体健康得分的字段

Double ofs;

And if you later want to sort members based on their overall fitness score, you can use Arrays.sort:

如果你以后想根据他们的整体健康得分对成员进行排序,你可以使用Arrays.sort

Arrays.sort(members, new OFSComparator());

回答by Nagaraja G Devadiga

You are breaking the signature of compareTo method. It should look like below,

您正在破坏 compareTo 方法的签名。它应该如下所示,

public int compareTo(FitnessScore o){
    return Double.compare(this.overall-o.overall);
}