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

来源:6-8 学生选课--- 实现学生序列排序

ZhengYQ

2015-08-05 19:29

/*

* 对其他类型泛型的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);

}

}


写回答 关注

0回答

还没有人回答问题,可以看看其他问题

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题