ArrayList集合学生对象遍历 输出语句的Student s = list.get(i);什么意思




```import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
public static void main(String[] args) {
        //创建List集合对象
 List<Student> list = new ArrayList<Student>();

        //创建学生对象
        Student s1 = new Student("大熊", 18);
        Student s2 = new Student("静香", 19);
        Student s3 = new Student("胖虎", 20);

        //添加学生对象到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);

 
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getName() + ", " + s.getAge());
        }
        System.out.println("-------------------------");

        
    }
}

/*
    学生类
*/

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}



img


欢迎采纳!

取出list对象里面的第i个元素。

 list.add(s1);
 list.add(s2);
 list.add(s3);

如list.get(0) 就是取出第一个元素s1
list.get(1) 就是取出第一个元素s2

萌新?实践是检验真理的唯一标准 跑一跑 你就知道了