zoukankan      html  css  js  c++  java
  • 集合的嵌套遍历

    public class Demo {
        public static void main(String[] args) {
            // 创建大集合
                    ArrayList<ArrayList<Student>> bigArrayList = new ArrayList<ArrayList<Student>>();
    
                    // 创建第一个班级的学生集合
                    ArrayList<Student> firstArrayList = new ArrayList<Student>();
                    // 创建学生
                    Student s1 = new Student("唐僧", 30);
                    Student s2 = new Student("孙悟空", 29);
                    Student s3 = new Student("猪八戒", 28);
                    Student s4 = new Student("沙僧", 27);
                    Student s5 = new Student("白龙马", 26);
                    // 学生进班
                    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("诸葛亮", 30);
                    Student s22 = new Student("司马懿", 28);
                    Student s33 = new Student("周瑜", 26);
                    // 学生进班
                    secondArrayList.add(s11);
                    secondArrayList.add(s22);
                    secondArrayList.add(s33);
                    // 把第二个班级存储到学生系统中
                    bigArrayList.add(secondArrayList);
    
                    // 创建第三个班级的学生集合
                    ArrayList<Student> thirdArrayList = new ArrayList<Student>();
                    // 创建学生
                    Student s111 = new Student("宋江", 40);
                    Student s222 = new Student("吴用", 35);
                    Student s333 = new Student("高俅", 30);
                    Student s444 = new Student("李师师", 22);
                    // 学生进班
                    thirdArrayList.add(s111);
                    thirdArrayList.add(s222);
                    thirdArrayList.add(s333);
                    thirdArrayList.add(s444);
                    // 把第三个班级存储到学生系统中
                    bigArrayList.add(thirdArrayList);
                    //嵌套增强for遍历
                    for(ArrayList<Student> array:bigArrayList){
                        for(Student s : array){
                            System.out.println(s.getName()+":"+s.getAge());
                        }
                    }
    
        }
    }
  • 相关阅读:
    [Usaco2013 DEC] Vacation Planning
    [Usaco2015 DEC] Counting Haybales
    [ZJOI 2008] 泡泡堂BNB
    [USACO17FEB]Why Did the Cow Cross the Road II
    [Usaco2018 Feb] New Barns
    [HNOI 2006] 鬼谷子的钱袋
    [Usaco2017 Feb]Why Did the Cow Cross the RoadII
    初涉数论分块
    「在更」初涉历史最值线段树
    初涉DSU on tree
  • 原文地址:https://www.cnblogs.com/flei/p/6689350.html
Copyright © 2011-2022 走看看