使用ArrayList存储学生并遍历

学生类为

  1. package com.itheima_01;
  2.  
  3. public class Student {
  4.  
  5. private int age;
  6. private String name;
  7.  
  8.  
  9.  
  10. public Student() {
  11. System.out.println("无参构造方法");
  12. }
  13. public Student(String name,int age) {
  14. this.name = name;
  15. this.age = age;
  16. }
  17. public void setName(String n) {
  18. name = n;
  19. }
  20.  
  21. public String getName() {
  22. return name;
  23. }
  24.  
  25. public void setAge(int a) {
  26. age = a;
  27. }
  28.  
  29. public int getAge() {
  30. return age;
  31. }
  32.  
  33. }

 

测试类为

  1. package com.itheima_01;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class ArrayListTest02 {
  6. public static void main(String[] args) {
  7. //创建集合对象
  8. ArrayList<Student> array = new ArrayList<Student>();
  9. //创建学生对象
  10. Student s1 = new Student("林青霞",30);
  11. Student s2 = new Student("风清扬",20);
  12. Student s3 = new Student("大司马",50);
  13. //添加学生对象到集合
  14. array.add(s1);
  15. array.add(s2);
  16. array.add(s3);
  17.  
  18. for(int i = 0;i<array.size();i++) {
  19. Student s = array.get(i);
  20. System.out.println(s.getName()+"----"+s.getAge());
  21. }
  22. }
  23.  
  24.  
  25. }

 ————————————————————————————-   —- – – – — – – – – — – – – — – – – — – – — – — – – — – – — – – –  —  –  — – – — – — – – —

ArrayList存储学生对象并遍历 案例 键盘输入版

Studet类

  1. package com.itheima03;
  2.  
  3. public class Student {
  4. private String name;
  5. private String age;
  6.  
  7. public Student() {
  8.  
  9. }
  10.  
  11. public Student(String name, String age) {
  12. this.name = name;
  13. this.age = age;
  14. }
  15.  
  16. public String getName() {
  17. return name;
  18. }
  19.  
  20. public String getAge() {
  21. return age;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25.  
  26. }
  27. public void setAge(String age) {
  28. this.age = age;
  29. }
  30. }

 测试类

  1. package com.itheima03;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class ArrayListTest03 {
  7. public static void main(String[] args) {
  8. ArrayList<Student> arr = new ArrayList<>();//创建集合对象
  9.  
  10. for(int i = 0;i<3;i++) {
  11. addStudent(arr);
  12. }
  13.  
  14. for(int i = 0;i<arr.size();i++) {
  15. Student x = arr.get(i);
  16. System.out.println(x.getName()+" "+x.getAge());
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. }
  25. public static void addStudent(ArrayList<Student> arr) {
  26. Scanner sc = new Scanner(System.in); //键盘录入学生需要的数据
  27.  
  28.  
  29. System.out.println("请输入学生姓名");
  30. String name = sc.nextLine();
  31.  
  32. System.out.println("请输入学生年龄");
  33. String age = sc.nextLine();
  34.  
  35.  
  36. //创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
  37. Student s = new Student();
  38. s.setName(name);
  39. s.setAge(age);
  40. //往集合中添加学生对象
  41. arr.add(s);
  42. }
  43. }

 

版权声明:本文为lsswudi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lsswudi/p/11393833.html