先创建学生类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(java.lang.String name, int age) {
this.name = name;
this.age = age;
}
public java.lang.String getName() {
return name;
}
public void setName(java.lang.String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
再创建测试类
import java.util.ArrayList;
public class StudentTest {
public static void main(String[] args) {
//创建对象
ArrayList<Student>list=new ArrayList<>();
//创建学生对象
Student s1=new Student("zhangsan",23);
Student s2=new Student("lisi",24);
Student s3=new Student("wangwu",25);
//添加元素
list.add(s1);
list.add(s2);
list.add(s3);
//遍历组合
for(int i=0;i<list.size();i++){
Student stu=list.get(i);
System.out.println(stu.getName()+","+stu.getAge());
}
}
}