Java - Collections.sort(列表)

时间:2014-11-25 21:48:29

标签: 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.nrCylindra >= headPosition) {
                higherNumbers.add(tmp);
            }
        }

        Collections.sort(higherNumbers);

        return higherNumbers;
    }

Collections.sort(higherNumbers)存在问题。 Any1知道为什么吗?

1 个答案:

答案 0 :(得分:1)

你应该使用泛型。因为如果里面的元素是“Comparable”,则只能对List进行排序。因此,您需要确保您的“应用程序”类是可比较的。 E.g:

public class Application implements Comparable<Application> {...}

然后您可以在&lt;&gt;中添加应用程序 - 括号。

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;
}
相关问题