继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门三:学生序列排序完善练习:生成3个不重复的1000以内正整数作为id

wkq
关注TA
已关注
手记 1
粉丝 1
获赞 1
public void testSort3(){
    List<Student> studentList = new ArrayList<Student>();
    List<Integer> integerList = new ArrayList<Integer>();
    Random random = new Random();
    Integer id;
    String[] names = {"Mike","Angela","Lucy"};
    for(int i = 0; i < 3; i++ ){
        do{
            id = random.nextInt(1000);
        }while(integerList.contains(id) || id.equals(0));
        integerList.add(id);
        studentList.add(new Student(id.toString(), names[i]));
    }
    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);
    }
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP