问答详情
源自:6-8 学生选课--- 实现学生序列排序

生成3个不重复的随机数,求指教,有没有跟简便的方法?给我看下。谢谢!

/*

* 对其他类型泛型的List进行排序,例如:Student

*/

public void testsort2() {

List<Student> studentList = new ArrayList<Student>();

List<Integer> IntegerList = new ArrayList<Integer>();

Random random = new Random();

Integer k;

for (int i = 0; i < 3; i++) {

do {

k = random.nextInt(10);

} while (IntegerList.contains(k));

IntegerList.add(k);

}

studentList.add(new Student(IntegerList.get(0) + "", "yyz"));

studentList.add(new Student(IntegerList.get(1) + "", "cl"));

studentList.add(new Student(IntegerList.get(2) + "", "zyq"));

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);

}

}


提问者:ZhengYQ 2015-08-05 19:29

个回答