import java.util.ArrayList;
public class text{
public static void main(String[] args) {
// 定义一个ArrayList集合,存放学生对象
ArrayList<Student> students = new ArrayList<>();
// 添加学生对象
students.add(new Student("张三", 18));
students.add(new Student("李四", 19));
students.add(new Student("王五", 20));
// 遍历集合,输出每个学生的姓名和年龄
for (Student student : students) {
System.out.println("姓名:"+student.getName()+",年龄:"+student.getAge());
}
}
}
// 学生类
class Student {
private String name; // 姓名
private int age; // 年龄
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
text 1
最新推荐文章于 2024-07-27 13:31:57 发布