手记

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

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);
    }
}
0人推荐
随时随地看视频
慕课网APP