package com.heima.arraylist;
import java.util.ArrayList;
import java.util.Iterator;
import com.heima.bean.Person;
public class Demo3_ArrayListArrayList {
/**
* 一个学校 两个学科 两个班级
*/
public static void main(String[] args) {
ArrayList<ArrayList<ArrayList<Person>>> school = new ArrayList<>(); //创建学校集合
// 创建班级
ArrayList<Person> one = new ArrayList<>(); //一班
one.add(new Person("科比", 21));
one.add(new Person("雷阿伦", 22));
one.add(new Person("艾弗森", 23));
ArrayList<Person> two = new ArrayList<>(); //二班
two.add(new Person("詹姆斯", 22));
two.add(new Person("安东尼", 23));
two.add(new Person("韦德", 24));
ArrayList<Person> three = new ArrayList<>(); //三班
three.add(new Person("邓肯", 22));
three.add(new Person("加内特", 23));
three.add(new Person("姚明", 24));
ArrayList<Person> four = new ArrayList<>(); //四班
four.add(new Person("库里", 22));
four.add(new Person("欧文", 23));
four.add(new Person("沃尔", 24));
ArrayList<ArrayList<Person>> java = new ArrayList<>(); // 创建学科
java.add(one);
java.add(two);
ArrayList<ArrayList<Person>> ios = new ArrayList<>(); // 创建学科
ios.add(three);
ios.add(four);
school.add(java); // 创建学校
school.add(ios);
/*
* 高级for循环
* for (ArrayList<ArrayList<Person>> s : school) { for
* (ArrayList<Person> x : s) { for (Person b : x) { } } }
* System.out.println(school);
*/
//迭代器
Iterator<ArrayList<ArrayList<Person>>> it = school.iterator();
while (it.hasNext()) {
Iterator<ArrayList<Person>> subIt = (Iterator<ArrayList<Person>>) it
.next().iterator();
while (subIt.hasNext()) {
Iterator<Person> subSubIt = (Iterator<Person>) subIt.next()
.iterator();
while (subSubIt.hasNext()) {
Person p = subSubIt.next();
System.out.println(p);
}
}
}
}
} |