• ArrayList


    使用ArrayList存储学生并遍历

    学生类为

    package com.itheima_01;
    
    public class Student {
    
        private int age;
        private String name;
    
    
    
        public Student() {
            System.out.println("无参构造方法");
        }
        public Student(String name,int age) {
            this.name = name;
            this.age = age;
        }
        public void setName(String n) {
            name = n;
        }
    
        public String getName() {
            return name;
        }
    
        public void setAge(int a) {
            age = a;
        }
    
        public int getAge() {
            return age;
        }
    
    }
    

    测试类为

    package com.itheima_01;
    
    import java.util.ArrayList;
    
    public class ArrayListTest02 {
        public static void main(String[] args) {
            //创建集合对象
            ArrayList<Student> array = new ArrayList<Student>();
            //创建学生对象
            Student s1 = new Student("林青霞",30);
            Student s2 = new Student("风清扬",20);
            Student s3 = new Student("大司马",50);
            //添加学生对象到集合
            array.add(s1);
            array.add(s2);
            array.add(s3);
    
            for(int i = 0;i<array.size();i++) {
                Student s = array.get(i);
                System.out.println(s.getName()+"----"+s.getAge());
            }
        }
    
    
    }
    

     -------------------------------------------------------------------------------------   ---- - - - -- - - - - -- - - - -- - - - -- - - -- - -- - - -- - - -- - - -  --  -  -- - - -- - -- - - --

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

    Studet类

    package com.itheima03;
    
    public class Student {
        private String name;
        private String age;
    
        public Student() {
    
        }
    
        public Student(String name, String age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public String getAge() {
            return age;
        }
        public  void setName(String name) {
            this.name = name;
    
        }
        public  void setAge(String age) {
            this.age = age;
        }
    }
    

     测试类

    package com.itheima03;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class ArrayListTest03 {
        public static void main(String[] args) {
            ArrayList<Student> arr = new ArrayList<>();//创建集合对象
    
            for(int i = 0;i<3;i++) {
                addStudent(arr);
            }
    
            for(int i = 0;i<arr.size();i++) {
                Student x = arr.get(i);
                System.out.println(x.getName()+" "+x.getAge());
            }
    
    
    
    
    
    
        }
        public static void addStudent(ArrayList<Student> arr) {
            Scanner sc = new Scanner(System.in);       //键盘录入学生需要的数据
    
    
            System.out.println("请输入学生姓名");
            String name = sc.nextLine();
    
            System.out.println("请输入学生年龄");
            String age = sc.nextLine();
    
    
            //创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
            Student s = new Student();
            s.setName(name);
            s.setAge(age);
            //往集合中添加学生对象
            arr.add(s);
        }
    }
    
  • 相关阅读:
    如何分配资源和管理资源
    让Project把周六和周日也计算工期
    Project设置子任务
    pytest-配置文件
    pytest-fixture
    pytest-标记
    pytest-断言
    pytest-参数化
    pytest入门
    maven-插件
  • 原文地址:https://www.cnblogs.com/lsswudi/p/11393833.html
Copyright © 2020-2023  润新知