集合嵌套之ArrayList嵌套ArrayList
阿新 • • 發佈:2017-08-23
嵌套 static color tarray print oid class lis bean
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 = newArrayList<>(); 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(newPerson("張三一", 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 }
集合嵌套之ArrayList嵌套ArrayList