package com.yjf.esupplier.common.test; import com.yjf.common.util.ToString; import java.util.ArrayList; /** * @author shusheng * @description 集合的嵌套遍历 * @Email shusheng@yiji.com * @date 2018/12/13 17:54 */ public class ArrayListDemo { public static void main(String[] args) { // 创建大集合 ArrayList<ArrayList<Student>> bigArrayList = new ArrayList<ArrayList<Student>>(); // 创建第一个班级的学生集合 ArrayList<Student> firstArrayList = new ArrayList<Student>(); //创建学生 Student s1 = new Student("唐僧", 31); Student s2 = new Student("孙悟空", 30); Student s3 = new Student("猪八戒", 29); Student s4 = new Student("沙僧", 28); Student s5 = new Student("白龙马", 27); // 学生进班 firstArrayList.add(s1); firstArrayList.add(s2); firstArrayList.add(s3); firstArrayList.add(s4); firstArrayList.add(s5); // 把第一个班级存储到学生系统中 bigArrayList.add(firstArrayList); // 创建第二个班级的学生集合 ArrayList<Student> secondArrayList = new ArrayList<Student>(); // 创建学生 Student s11 = new Student("诸葛亮", 31); Student s22 = new Student("司马懿", 32); Student s33 = new Student("周瑜", 33); // 学生进班 secondArrayList.add(s11); secondArrayList.add(s22); secondArrayList.add(s33); // 把第二个班级存储到学生系统中 bigArrayList.add(secondArrayList); // 创建第三个班级的学生集合 ArrayList<Student> thirdArrayList = new ArrayList<Student>(); // 创建学生 Student s111 = new Student("宋江", 44); Student s222 = new Student("吴用", 43); Student s333 = new Student("高俅", 42); Student s444 = new Student("李师师", 41); // 学生进班 thirdArrayList.add(s111); thirdArrayList.add(s222); thirdArrayList.add(s333); thirdArrayList.add(s444); // 把第三个班级存储到学生系统中 bigArrayList.add(thirdArrayList); // 遍历集合 for (ArrayList<Student> array : bigArrayList) { for (Student s : array) { System.out.println(s.getName() + "---" + s.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 void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return ToString.toString(this); } }