StudentManager.java 1.5 KB

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