为了账号安全,请及时绑定邮箱和手机立即绑定

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

}

}


正在回答

1 回答

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信