用List接口创建一个20个100以内数字的有序集合
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
?
public class Test2 {
?public static void main(String[] args) {
??Random ran = new Random();
??List list = new ArrayList();
??while (true) {
???int n = ran.nextInt(100);
???if (!list.contains(n)) {
????list.add(n);
???}
???if (list.size() == 20) {
????break;
???}
??}
??
// 排序
??System.out.println(list);
??for (int i = 0; i < list.size() - 1; i++) {
???for (int j = i + 1; j < list.size(); j++) {
????int n1 = Integer.parseInt(list.get(i).toString());
????int n2 = Integer.parseInt(String.valueOf(list.get(j)));
????if (n1 > n2) {
?????list.set(i, n2);
?????list.set(j, n1);
????}
???}
??}
??
??System.out.println(list);
?}
}
?
打印结果:
[70, 89, 80, 12, 2, 40, 57, 9, 4, 32, 42, 47, 36, 1, 15, 21, 49, 29, 38, 45]
[1, 2, 4, 9, 12, 15, 21, 29, 32, 36, 38, 40, 42, 45, 47, 49, 57, 70, 80, 89]