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);
}
}
1111111111111
你这随机正整数是会重复的,你把1000改成2,然后就可以验证到是会重复的,我想是studentList.contains(k)这里有问题,即使随机数重复了,这里结果也是float
你这代码有问题把