importjava.util.*;
public class test{
ArrayList al;
public test(int num, intmod) {
al = newArrayList(num);
Random rand = newRandom();
System.out.println("TheArrayList Sort Before:");
for (int i = 0; i <num; i++) {
al.add(newInteger(Math.abs(rand.nextInt()) % mod + 1));
System.out.println("al[" +i + "]=" + al.get(i));
}
}
public void SortIt(){
IntegertempInt;
int MaxSize =1;
for (int i = 1; i <al.size(); i++) {
tempInt = (Integer)al.remove(i);
if (tempInt.intValue()>= ((Integer) al.get(MaxSize - 1))
.intValue()){
al.add(MaxSize,tempInt);
MaxSize++;
System.out.println(al.toString());
} else {
for (int j = 0; j <MaxSize; j++) {
if(((Integer)al.get(j)).intValue() >= tempInt.intValue()) {
al.add(j,tempInt);
MaxSize++;
System.out.println(al.toString());
break;
}
}
}
}
System.out.println("TheArrayList Sort After:");
for (int i = 0; i <al.size(); i++) {
System.out.println("al[" +i + "]=" + al.get(i));
}
}
public static voidmain(String[] args) {
test is = new test(10,100);
is.SortIt();
}
}
输出结果:
The ArrayList Sort Before:
al[0]=79
al[1]=81
al[2]=14
al[3]=100
al[4]=92
al[5]=76
al[6]=5
al[7]=55
al[8]=39
al[9]=36
[79, 81, 14, 100, 92, 76, 5, 55, 39, 36]
[14, 79, 81, 100, 92, 76, 5, 55, 39, 36]
[14, 79, 81, 100, 92, 76, 5, 55, 39, 36]
[14, 79, 81, 92, 100, 76, 5, 55, 39, 36]
[14, 76, 79, 81, 92, 100, 5, 55, 39, 36]
[5, 14, 76, 79, 81, 92, 100, 55, 39, 36]
[5, 14, 55, 76, 79, 81, 92, 100, 39, 36]
[5, 14, 39, 55, 76, 79, 81, 92, 100, 36]
[5, 14, 36, 39, 55, 76, 79, 81, 92, 100]
The ArrayList Sort After:
al[0]=5
al[1]=14
al[2]=36
al[3]=39
al[4]=55
al[5]=76
al[6]=79
al[7]=81
al[8]=92
al[9]=100
输出结果:
The ArrayList Sort Before:
al[0]=79
al[1]=81
al[2]=14
al[3]=100
al[4]=92
al[5]=76
al[6]=5
al[7]=55
al[8]=39
al[9]=36
[79, 81, 14, 100, 92, 76, 5, 55, 39, 36]
[14, 79, 81, 100, 92, 76, 5, 55, 39, 36]
[14, 79, 81, 100, 92, 76, 5, 55, 39, 36]
[14, 79, 81, 92, 100, 76, 5, 55, 39, 36]
[14, 76, 79, 81, 92, 100, 5, 55, 39, 36]
[5, 14, 76, 79, 81, 92, 100, 55, 39, 36]
[5, 14, 55, 76, 79, 81, 92, 100, 39, 36]
[5, 14, 39, 55, 76, 79, 81, 92, 100, 36]
[5, 14, 36, 39, 55, 76, 79, 81, 92, 100]
The ArrayList Sort After:
al[0]=5
al[1]=14
al[2]=36
al[3]=39
al[4]=55
al[5]=76
al[6]=79
al[7]=81
al[8]=92
al[9]=100
JAVA类实现序例化的方法是实现java.io.Serializable接口
Collection框架中实现比较要实现Comparable 接口和 Comparator 接口
转载自:http://blog.sina.com.cn/s/blog_647a492a0101bzn9.html
转载自:http://blog.sina.com.cn/s/blog_647a492a0101bzn9.html