import java.util.ArrayList;
/*编写程序,生成5个1至10之间的随机整数,存入一个List集合,
* 编写方法对List集合进行排序(自定义算法,禁用Collections.sort方法和TreeSet),
* 然后遍历集合输出。
*/
public class Test02 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int x = 0; x < 5; x++) {
int y = (int) (Math.random() * 10);
list.add(y);
}
PaiXu(list);
for (Integer i : list) {
System.out.println(i);
}
}
public static void PaiXu(ArrayList<Integer> list) {
for (int x = 0; x < list.size() - 1; x++) {
for (int y = 0; y < list.size() - 1 - x; y++) {
if (list.get(y) > list.get(y + 1)) {
int temp = list.get(y);
list.set(y, list.get(y + 1));
list.set(y + 1, temp);
}
}
}
}
} |
|