Java 要根据属性值对ArrayList排序吗
我有一个结果填充的ArrayList,每个结果都有一个名为resultValue的属性,它存储一个double。我想根据属性resultValue的值对ArrayList进行排序,并能够像这样打印出来:Java 要根据属性值对ArrayList排序吗,java,sorting,arraylist,collections,compareto,Java,Sorting,Arraylist,Collections,Compareto,我有一个结果填充的ArrayList,每个结果都有一个名为resultValue的属性,它存储一个double。我想根据属性resultValue的值对ArrayList进行排序,并能够像这样打印出来: public int compareTo(Result otherResult) { if (this.resultValue > otherResult.resultValue()) { return 1; } else if (this.re
public int compareTo(Result otherResult) {
if (this.resultValue > otherResult.resultValue()) {
return 1;
} else if (this.resultValue < otherResult.resultValue()) {
return -1;
} else {
return 0;
}
}
public int compareTo(Result otherResult) {
if (this.resultValue > otherResult.resultValue()) {
return 1;
} else if (this.resultValue < otherResult.resultValue()) {
return -1;
} else {
return 0;
}
}
public int compareTo(Result otherResult){
if(this.resultValue>otherResult.resultValue()){
返回1;
}else if(this.resultValue
在包含存储结果的ArrayList的类中,我使用了以下方法:
void typeOutOrderedList() {
Collections.sort(resultlist);
for (int i = 0; i < resultlist.size(); i++) {
Result res = resultlist.get(i);
System.out.println(res.competitionStartNumber() + " has the result" + res.resultValue());
}
}
void typeOutOrderedList(){
Collections.sort(resultlist);
对于(int i=0;i
你重新发明了轮子;使用JDK使代码更简单易读:
public int compareTo(Result otherResult) {
return Double.compare(otherResult.resultValue(), resultValue());
}
还要注意传递给
compare()
的参数顺序-这将为您提供所需的顺序(降序)。您已经重新发明了控制盘;使用JDK使代码更简单易读:
public int compareTo(Result otherResult) {
return Double.compare(otherResult.resultValue(), resultValue());
}
还要注意传递给
compare()
的参数的顺序-这将为您提供所需的顺序(降序)。您是否制作了Result
实现了Comparable
接口?@Vlad public class Result实现了Comparable Ihave@A.Bohlund隐马尔可夫模型。。。6之前的4是您的比较实现的顺序。交换返回-1和1@A.Bohlund是的,交换1和-1是否实现了Result
可比接口?@Vlad公共类Result实现了可比接口have@A.Bohlund隐马尔可夫模型。。。6之前的4是您的比较实现的顺序。交换返回-1和1@A.Bohlund是的,交换1和-1