Java ArrayList 點去 sort

2008-05-03 12:39 am
I have a ArrayList<Integer>
This contain <50,10,20,87,41>

我要用java build in 左既 quicksort method 去 sort, 個method 叫咩名?

如:
ArrayList<Integer> a1 = new ArrayList<Integer>();

a1 點 sort?  

用Connections.sort(a1) ?

但係佢又有error wor.

請各位高手指教. 要俾code

回答 (1)

2008-05-03 6:47 pm
✔ 最佳答案
import java.util.ArrayList;
public class SortTest
{
public static void print(ArrayList a)
{
for(int i=0;i&lt; a.size();i++)
System.out.printf(&quot;%d &quot;,a.get(i));
System.out.println();
}
public static void main(String[] args)
{
ArrayList&lt; Integer&gt; a=new ArrayList&lt; Integer&gt;();
a.add(50);
a.add(10);
a.add(20);
a.add(87);
a.add(41);
print(a);
java.util.Collections.sort(a);
print(a);
}
}

Output:
50 10 20 87 41
10 20 41 50 87
Press any key to continue . . .


收錄日期: 2021-04-13 15:31:07
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080502000051KK01534

檢視 Wayback Machine 備份