ArrayList를 정렬하여 가장 작은 것부터 가장 큰 것까지 정렬하려고합니다. 다음 코드를 가지고 있습니다 :ArrayList <BigInteger> 정렬 알고리즘 Java
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를 정렬합니까?
'Collections.sort()'를 사용하지 않는 이유가 있습니까? 자기 개선 운동입니까? – amit
@amit Collections.Sort()가 있습니까? 나는 몰랐다. 고마워,하지만 어쨌든 여분의 장소는 어디에서 제거됩니까? – Justin
FAQ (자주 묻는 질문) - http://stackoverflow.com/faq#close> too localized – djechlin