【集合的嵌套遍歷】
阿新 • • 發佈:2019-01-31
tde tar get static string sheng array ide 豬八戒
package com.yjf.esupplier.common.test; import com.yjf.common.util.ToString; import java.util.ArrayList; /** * @author shusheng * @description 集合的嵌套遍歷 * @Email [email protected] * @date 2018/12/13 17:54 */ public class ArrayListDemo { public static void main(String[] args) { // 創建大集合 ArrayList<ArrayList<Student>> bigArrayList = newArrayList<ArrayList<Student>>(); // 創建第一個班級的學生集合 ArrayList<Student> firstArrayList = new ArrayList<Student>(); //創建學生 Student s1 = new Student("唐僧", 31); Student s2 = new Student("孫悟空", 30); Student s3 = new Student("豬八戒", 29); Student s4= new Student("沙僧", 28); Student s5 = new Student("白龍馬", 27); // 學生進班 firstArrayList.add(s1); firstArrayList.add(s2); firstArrayList.add(s3); firstArrayList.add(s4); firstArrayList.add(s5); // 把第一個班級存儲到學生系統中 bigArrayList.add(firstArrayList);// 創建第二個班級的學生集合 ArrayList<Student> secondArrayList = new ArrayList<Student>(); // 創建學生 Student s11 = new Student("諸葛亮", 31); Student s22 = new Student("司馬懿", 32); Student s33 = new Student("周瑜", 33); // 學生進班 secondArrayList.add(s11); secondArrayList.add(s22); secondArrayList.add(s33); // 把第二個班級存儲到學生系統中 bigArrayList.add(secondArrayList); // 創建第三個班級的學生集合 ArrayList<Student> thirdArrayList = new ArrayList<Student>(); // 創建學生 Student s111 = new Student("宋江", 44); Student s222 = new Student("吳用", 43); Student s333 = new Student("高俅", 42); Student s444 = new Student("李師師", 41); // 學生進班 thirdArrayList.add(s111); thirdArrayList.add(s222); thirdArrayList.add(s333); thirdArrayList.add(s444); // 把第三個班級存儲到學生系統中 bigArrayList.add(thirdArrayList); // 遍歷集合 for (ArrayList<Student> array : bigArrayList) { for (Student s : array) { System.out.println(s.getName() + "---" + s.getAge()); } } } } class Student { private String name; private int age; public Student(String name, int age) { this.name=name; this.age=age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return ToString.toString(this); } }
【集合的嵌套遍歷】