我想对 ArrayList 进行排序,使其从最小到最大。我有以下代码:
public static ArrayList<BigInteger> sortBigInteger(ArrayList<BigInteger> toSort){
ArrayList<BigInteger> toReturn = new ArrayList<BigInteger>();
toReturn.add(toSort.remove(0));
for (int i = 0; i < toSort.size(); i++){
BigInteger n = toSort.get(i);
boolean eval = false;
in: for (int a = 0; a < toReturn.size(); a++){
if (n.compareTo(toReturn.get(a)) < 0){
toReturn.add(a, toSort.remove(i));
eval = true;
break in;
}
}
if (!eval) toReturn.add(toSort.remove(i));
}
toSort = toReturn;
return toReturn;
}
但是,我失去了元素。使用大小为 32 的 ArrayList,我得到大小为 15 的 ArrayList。额外的删除发生在哪里?
主要问题:如何对 ArrayList 进行排序?