class Student {
    private int id;
    private String name;
    private int score;
    public Student(int id, String name, int score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public int getScore() {
        return score;
    }
}

class Test {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        Random rand = new Random();
        for (int i = 0; i < 6; i++) {
            int score = rand.nextInt(100);
            students.add(new Student(i, "学生" + i, score));
        }
        // 修改ArrayList中第2个Student的成绩为98
        students.get(1).score = 98;
        // 删除ArrayList中第3个Student
        students.remove(2);
        // 对ArrayList中的所有学生进行遍历,统一将成绩加2
        for (Student student : students) {
            student.score += 2;
        }
        // 对ArrayList中的所有学生按照成绩降序排序
        students.sort((s1, s2) -> s2.score - s1.score);
    }
}
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐