1 import java.util.ArrayList; 2 3 import com.heima.bean.Person; 4 5 public class Demo5_ArrayListArrayList { 6 /* 7 * 集合嵌套之ArrayList嵌套ArrayList 案例:一个学校有好多班级,一个班级又有好多人;遍历输出这个学校的所有人 8 */ 9 public static void main(String[] args) { 10 ArrayList<ArrayList<Person>> school = new ArrayList<>(); 11 ArrayList<Person> class1 = new ArrayList<>(); 12 class1.add(new Person("张三", 23)); 13 class1.add(new Person("李四", 24)); 14 class1.add(new Person("王五", 25)); 15 ArrayList<Person> class2 = new ArrayList<>(); 16 class2.add(new Person("张三一", 23)); 17 class2.add(new Person("李四一", 24)); 18 class2.add(new Person("王五一", 25)); 19 school.add(class1); 20 school.add(class2); 21 for (ArrayList<Person> outer : school) { 22 for (Person in : outer) { 23 System.out.println(in); 24 } 25 } 26 27 } 28 29 }