zoukankan      html  css  js  c++  java
  • Java中集合的嵌套

    集合的嵌套遍历
    获取10个1-20之间的随机数,要求不能重复
    键盘录入多个数据,以0结束,要求在控制台输出这多个数据的最大值。

    public static void main(String[] args){
    //创建大集合
    ArrayList<ArrayList<Student>> bigArrayList = new ArrayList<ArrayList<Student>>();

    //创建第一个班级的学生集合
    ArrayList<Student> firstArrayList = new ArrayList<Student>();

    //创建学生
    Student s1 = new Student("学生1",20);
    Student s2 = new Student("学生1",20);
    Student s3 = new Student("学生1",20);
    Student s4 = new Student("学生1",20);
    Student s5 = new Student("学生1",20);

    //学生进班
    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 s6 = new Student("学生2",20);
    Student s7 = new Student("学生2",20);
    Student s8 = new Student("学生2",20);
    Student s9 = new Student("学生2",20);
    Student s10 = new Student("学生2",20);

    //学生进班
    secondArrayList.add(s6);
    secondArrayList.add(s7);
    secondArrayList.add(s8);
    secondArrayList.add(s9);
    secondArrayList.add(s10);

    //把第二个班级存储到学生系统中
    bigArrayList.add(secondArrayList);

    //创建第三个班级的学生集合
    ArrayList<Student> thirdArrayList = new ArrayList<Student>();

    //创建学生
    Student s11 = new Student("学生3",20);
    Student s12= new Student("学生3",20);
    Student s13 = new Student("学生3",20);
    Student s14 = new Student("学生3",20);
    Student s15 = new Student("学生3",20);

    //学生进班
    thirdArrayList.add(s11);
    thirdArrayList.add(s12);
    thirdArrayList.add(s13);
    thirdArrayList.add(s14);
    thirdArrayList.add(s15);

    //把第三个班级存储到学生系统中
    bigArrayList.add(thirdArrayList);

    //遍历集合
    for(ArrayList<Student>array:bigArrayList){
    for(Student s:array){
    System.out.println(s.getName()+"----"+s.getAge());
    }
    }
    }

  • 相关阅读:
    洛谷P1056_排座椅 贪心+桶排思想
    NOIP普及组相关
    洛谷P1464_Function 记忆化搜索
    Xcode的使用技巧
    MAC_XCODE清理
    输入框跟随键盘移动效果的实现
    #pragma的进阶用法
    iOS 逆向
    警告框
    UIImageView设置圆角的方式
  • 原文地址:https://www.cnblogs.com/lszbk/p/12318397.html
Copyright © 2011-2022 走看看