package Code429;
import java.util.ArrayList;
public class CodeArrayListStudent {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
Student one = new Student("卢本伟", 18);
Student two = new Student("Faker", 16);
//将学生名字,年龄 用add方法加入列表中
list.add(one);
list.add(two);
for (int i = 0; i < list.size(); i++) {
//遍历列表
Student stu = list.get(i);
System.out.println("name is:"+stu.getName()+",the age is "+stu.getAge());
}
}
//定义一个学生类
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//使用 getter 和setter 方法
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;
}
}
}
运行代码↓