出逃指南
2022-04-10 17:04
public void testsort4() { List<Student> studentList = new ArrayList<Student>(); List<String> idList = new ArrayList<String>(); Random random=new Random(); for (int i=0;i<3;i++){ StringBuilder stringBuilder; do {stringBuilder=new StringBuilder(); Integer num=random.nextInt(1000); String str=num.toString(); stringBuilder.append(str); }while (idList.contains(stringBuilder)); idList.add(stringBuilder.toString()); } studentList.add(new Student(idList.get(0), "Mike")); studentList.add(new Student(idList.get(1), "Angela")); studentList.add(new Student(idList.get(2), "Peter")); System.out.println("------------------排序前-----------------------------"); for (Student a : studentList) { System.out.println("学生:" + a.id + "," + a.name); } System.out.println("---------------按ID排序后-----------------------------"); Collections.sort(studentList); for (Student a : studentList) { System.out.println("学生:" + a.id + "," + a.name); } System.out.println("---------------按name排序后---------------------------"); Collections.sort(studentList,new StudentComparator()); for (Student a : studentList) { System.out.println("学生:" + a.id + "," + a.name); } }
还没有人回答问题,可以看看其他问题
Java入门第三季
409787 学习 · 4340 问题
相似问题