优化1000以内重复的随机数
public void testSortStudent() {
List<Student> studentList=new ArrayList<Student>();
List<Integer> iList=new ArrayList<Integer>();
Random random=new Random();
Integer k;
for(int j=0;j<3;j++){//随机生成3个随机数
do {
k=random.nextInt(1000);//生成1000以内的随机正整数
}while(iList.contains(k));
iList.add(k);
}
studentList.add(new Student(iList.get(0)+"", "Mike"));
studentList.add(new Student(iList.get(1)+"", "Black"));
studentList.add(new Student(iList.get(2)+"", "lucy"));
System.out.println("-------排序前--------");
for(Student st:studentList) {
System.out.println("学生:"+st.getId()+":"+st.getName());
}
System.out.println("-------排序后--------");
//解决sort处报错,studentList是一个Student泛型的序列,因为Student类未实现Comparable
Collections.sort(studentList);
for(Student st:studentList) {
System.out.println("学生:"+st.getId()+":"+st.getName());
}
}