天天看點

第三次學JAVA再學不好就吃翔(part88)--ArrayList嵌套ArrayList

學習筆記,僅供參考,有錯必糾

ArrayList嵌套ArrayList

  • 舉個例子
package com.guiyang.object;

import java.sql.Array;
import java.util.ArrayList;


import com.guiyang.bean.People;

public class Demo5_ArrayListArrayList {

  public static void main(String[] args) {
    ArrayList<ArrayList<People>> list = new ArrayList<>();
    
    ArrayList<People> first = new ArrayList<>();
    first.add(new People("Ada", 21));
    first.add(new People("Jack", 22));
    first.add(new People("Petty", 19));
    
    ArrayList<People> second = new ArrayList<>();
    second.add(new People("小白", 18));
    second.add(new People("小黑", 19));
    
    list.add(first);
    list.add(second);
    
    for (ArrayList<People> arrayList : list) {
      for (People people : arrayList) {
        System.out.println(people);
      } 
    }
  }
}      
People [name=Ada, age=21]
People [name=Jack, age=22]
People [name=Petty, age=19]
People [name=小白, age=18]
People [name=小黑, age=19]