生成3个不重复的1000以内的随机正整数作为学生ID的程序,请多多指教!!!
public void sort4(){
List<Student> studentList=new ArrayList<Student>();
String s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY";
Random random=new Random();
Integer k;
for(int i=0;i<3;i++){
StringBuffer str=new StringBuffer();
do{
k=random.nextInt(1000);
for(int j=0;j<4j++){
int num=random.nextInt(s.length());
str.append(s.charAt(num)); }
}while(studentList.contains(k)&&studentList.contains(str.toString()));
studentList.add(new Student(k+"",str.toString()));
}
System.out.println("------------排序前-----------------");
for(Student student:studentList){
System.out.println("学生:"+student.id+","+student.name);
}
Collections.sort(studentList);
System.out.println("------------排序后-----------------");
for(Student student:studentList){
System.out.println("学生:"+student.id+","+student.name);
}
Collections.sort(studentList,new StudentComparator());
System.out.println("--------------按照姓名排序后----------------");
for(Student student:studentList){
System.out.println("学生:"+student.id+","+student.name);
}
}