集合的嵌套遍历

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @program: javalearnCode
 * @description: 集合的嵌套遍历
 * @author: chen
 * @create: 2018-08-24 16:44
 **/
public class Demo1 {
    public static void main(String[] args){
        //创建大集合
        ArrayList> c= new ArrayList>();

        //创建小集合:各个班级
        ArrayList class1=new ArrayList();
        class1.add(new Student("关云",45));
        class1.add(new Student("刘备",50));
        class1.add(new Student("张飞",48));
        //添加班级小集合到年级大集合
        c.add(class1);//-----------忘了给年级添加班级

        ArrayList class2=new ArrayList();
        class2.add(new Student("林黛玉",23));
        class2.add(new Student("薛宝钗",24));
        class2.add(new Student("贾宝玉",24));
        c.add(class2);

        ArrayList class3=new ArrayList();
        class3.add(new Student("孙悟空",25));
        class3.add(new Student("猪八戒",23));
        class3.add(new Student("沙僧",25));
        c.add(class3);


        //普通for遍历
        for(int i=0; i classx= c.get(i);
            for(int j=0;j"---"+s.getAge());
            }
        }

        //增强for遍历
        for(ArrayList classi: c){
            for(Student s: classi){
                System.out.println(s.getName()+"---"+s.getAge());
            }
        }
    }
}

你可能感兴趣的:(java)