package com.heima.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class Test1 {
/**
* (1)生成10个1至100之间的随机整数(不能重复),存入一个List集合(可以先不适用泛型,泛型明天讲)
* (2)编写方法对List集合进行排序,禁用Collections.sort方法和TreeSet (2)然后利用迭代器遍历集合元素并输出
* (3)如:15 18 20 40 46 60 65 70 75 91
*/
public static void main(String[] args) {
Random s = new Random();
List s1 = new ArrayList();
while( s1.size()<10 ){
if (s1.contains(s.nextInt(100) + 1)) {
continue;
}
s1.add(s.nextInt(100) + 1);
}
//System.out.println(s1);
//bubbleSort(arr1);
Object[] arr = s1.toArray();
Integer[] arr1 = (Integer[])arr;
bubbleSort(arr1);
List<Integer> s2 = Arrays.asList(arr1);
System.out.println(s2);
}
public static void bubbleSort(Integer[] arr1) {
for (int i = 0; i < arr1.length-1; i++) {
for (int j = 0; j < arr1.length-1-i; j++) {
if (arr1[j] > arr1[j + 1]) {
Integer temp = arr1[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = temp;
}
}
}
}
}
|
|