集合嵌套之ArrayList嵌套ArrayList遍歷學習筆記
阿新 • • 發佈:2018-10-10
vpd com ado 技術分享 alt con 51cto shadow println
/**
* * A:案例演示
* 集合嵌套之ArrayList嵌套ArrayList
* 案例:
* 我們學科,學科又分為若個班級
* 整個學科一個大集合
* 若幹個班級分為每一個小集合
*/
@Test
public void twoArrary() {
ArrayList<ArrayList<Person>> list = new ArrayList<>();
ArrayList<Person> first = new ArrayList<>();
first.add(new Person("楊一冪", 30));
first.add(new Person("李冰", 33));
first.add(new Person("範冰", 20));
ArrayList<Person> second = new ArrayList<>();
second.add(new Person("黃明", 31));
second.add(new Person("趙添薇", 33));
second.add(new Person("陳坤一", 32));
//將班級添加學科中去
list.add(first);
list.add(second);
//遍歷學科集合
for (ArrayList<Person> allperson : list) {
for (Person person : allperson) {
System.out.println(person);
}
}
}
集合嵌套之ArrayList嵌套ArrayList遍歷學習筆記