1. 程式人生 > >java:集合框架(集合巢狀之ArrayList巢狀ArrayList)

java:集合框架(集合巢狀之ArrayList巢狀ArrayList)

import java.util.ArrayList;
import com.heima.bean.person;
/*案列演示:
學科一個大集合
一個學科裡面若干個班級(一個班級是一個小集合)*/

public class Demo4_ArrayList {

	public static void main(String[] args) {
		ArrayList<ArrayList<person>> list=new ArrayList<>();
		ArrayList<person>  first=new ArrayList<>();//建立第一個班級
		first.add(new person("小明",21));
		first.add(new person("小名",22));
		first.add(new person("小命",23));
		
		ArrayList<person>  second=new ArrayList<>();//建立第二個班級
		
		second.add(new person("小明2",21));
		second.add(new person("小名2",22));
		second.add(new person("小命2",23));
		
		
		list.add(first);
		list.add(second);
//		遍歷學科集合
		for (ArrayList<person> ap : list) {
			for (person p : ap) {
				System.out.println(p);
			}
			
		}
		
	}

}