第一部分:
//创建学生类 public class Student { //成员变量 private String name; private int age; //构造方法 public Student(){ super(); } 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } 第二部分:
public class Demo10 { public static void main(String[] args){ //创建一个大集合 ArrayList> bigArrayList = new ArrayList<>(); //创建第一个组的集合 ArrayList firstClass = new ArrayList<>(); //创建学生对象 Student s1 = new Student("乔峰",30); Student s2 = new Student("段誉",28); Student s3 = new Student("虚竹",25); //添加对象 firstClass.add(s1); firstClass.add(s2); firstClass.add(s3); //添加到最大的结集合中去 bigArrayList.add(firstClass); //创建第二个组的集合 ArrayList secoundClass = new ArrayList<>(); //创建学生对象 Student s4 = new Student("唐僧",30); Student s5 = new Student("悟空",28); Student s6 = new Student("八戒",25); Student s7 = new Student("沙僧",25); //添加对象 secoundClass.add(s4); secoundClass.add(s5); secoundClass.add(s6); secoundClass.add(s7); //添加到最大的结集合中去 bigArrayList.add(secoundClass); //遍历最大的集合 for (ArrayList array:bigArrayList){ for (Student s:array){ System.out.println(s.getName()+"--------"+s.getAge()); } } } }