| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package exericse.exericse01;
- import java.util.ArrayList;
- /**
- * @author WanJl
- * @version 1.0
- * @title StudentManager
- * @description
- * @create 2026/7/29
- */
- public class StudentManager {
- public static void main(String[] args) {
- // 1. 创建一个 ArrayList,用于存储 Student 对象
- // 提示:ArrayList<Student> students = new ArrayList<>();
- ArrayList<Student> students = new ArrayList<>();
- // 2. 创建 4 个 Student 对象,信息如下:
- // 张三, 20, S001
- // 李四, 21, S002
- // 王五, 19, S003
- // 赵六, 22, S004
- Student stu1 = new Student("张三", 20, "S001");
- Student stu2 = new Student("李四", 21, "S002");
- Student stu3 = new Student("王五", 19, "S003");
- Student stu4 = new Student("赵六", 22, "S004");
- // 3. 将 4 个学生对象添加到 ArrayList 中
- students.add(stu1);
- students.add(stu2);
- students.add(stu3);
- students.add(stu4);
- // 4. 输出 ArrayList 中的学生总数(使用 size() 方法)
- System.out.println("学生总数:"+students.size());
- // 5. 遍历 ArrayList,打印每个学生的信息(使用 for 循环 + get() 方法)
- // 输出格式:学号:S001, 姓名:张三, 年龄:20
- for (int i = 0; i < students.size(); i++) {
- Student student = students.get(i);
- System.out.println(student);
- }
- }
- }
|