Java-Collections.sort(列表)
我在运行此方法时遇到问题:Java-Collections.sort(列表),java,sorting,collections,integer,Java,Sorting,Collections,Integer,我在运行此方法时遇到问题: public List getHigherNumber() { List higherNumbers = new ArrayList(); for (int i = 0; i < ApplicationList.size(); i++) { Application tmp = (Application) ApplicationList.get(i); if (tmp.nrCylin
public List getHigherNumber() {
List higherNumbers = new ArrayList();
for (int i = 0; i < ApplicationList.size(); i++) {
Application tmp = (Application) ApplicationList.get(i);
if (tmp.nrCylindra >= headPosition) {
higherNumbers.add(tmp);
}
}
Collections.sort(higherNumbers);
return higherNumbers;
}
public List getHigherNumber(){
List higherNumbers=new ArrayList();
对于(int i=0;i=头位){
高级数字。添加(tmp);
}
}
集合。排序(高数);
返回较高的数字;
}
Collections.sort(高数)有问题。有人知道为什么吗?你应该使用泛型。因为只有当列表中的元素“可比较”时,列表才能排序。因此,您需要确保您的“应用程序”类具有可比性。例如:
public class Application implements Comparable<Application> {...}
公共类应用程序实现可比较的{…}
然后可以将应用程序添加到-括号中
public List<Application> getHigherNumber() {
List<Application> higherNumbers = new ArrayList<>();
for (int i = 0; i < ApplicationList.size(); i++) {
Application tmp = (Application) ApplicationList.get(i);
if (tmp.nrCylindra >= headPosition) {
higherNumbers.add(tmp);
}
}
Collections.sort(higherNumbers);
return higherNumbers;
}
public List getHigherNumber(){
List higherNumbers=new ArrayList();
对于(int i=0;i=头位){
高级数字。添加(tmp);
}
}
集合。排序(高数);
返回较高的数字;
}
问题是?啊,我们很久以前在那个地方遇到的一个问题。嘿,你看到什么问题了?什么问题?如果有错误,请将其添加到您的问题中。应用程序实现可比较?什么是应用程序
,是否可比较?或者使用比较器
。没有必要更改类定义。