A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 马赛 中级黑马   /  2015-12-13 22:53  /  430 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.ArrayList;

  2. public class ArrayListDemo {
  3.        
  4.         public static void main(String[] args) {
  5.                
  6.                 /**
  7.                  * 使用集合存储多个班级的集合,然后每一个班级集合的元素应该是学生对象
  8.                  */
  9.                 ArrayList<ArrayList<Student>> al = new ArrayList<ArrayList<Student>>() ;
  10.                
  11.                 // 创建基本班集合对象
  12.                 ArrayList<Student> jcAl = new ArrayList<Student>() ;
  13.                
  14.                 // 添加元素
  15.                 jcAl.add(new Student("张三1" , 23)) ;
  16.                 jcAl.add(new Student("李四1" , 24)) ;
  17.                 jcAl.add(new Student("王五1" , 25)) ;
  18.                 jcAl.add(new Student("赵六1" , 26)) ;
  19.                
  20.                 // 把jcAl添加到al中
  21.                 al.add(jcAl) ;
  22.                
  23.                 // 创建UI班集合对象
  24.                 ArrayList<Student> uiAl = new ArrayList<Student>() ;
  25.                
  26.                 // 添加元素
  27.                 uiAl.add(new Student("张三" , 23)) ;
  28.                 uiAl.add(new Student("李四" , 24)) ;
  29.                 uiAl.add(new Student("王五" , 25)) ;
  30.                 uiAl.add(new Student("赵六" , 26)) ;
  31.                
  32.                 // 把uiAl添加到al中
  33.                 al.add(uiAl) ;
  34.                
  35.                 // 遍历
  36.                 // ArrayList<ArrayList<Student>> al = new ArrayList<ArrayList<Student>>() ;
  37.                 for(ArrayList<Student> a : al){
  38.                         for(Student s : a){
  39.                                 System.out.println(s.getName() + "---" + s.getAge());
  40.                         }
  41.                         System.out.println();
  42.                 }
  43.         }

  44. }
复制代码
  1. public class Student {

  2.         private String name ;
  3.        
  4.         private int age ;

  5.         public Student() {
  6.                 super();
  7.                 // TODO Auto-generated constructor stub
  8.         }

  9.         public Student(String name, int age) {
  10.                 super();
  11.                 this.name = name;
  12.                 this.age = age;
  13.         }

  14.         public String getName() {
  15.                 return name;
  16.         }

  17.         public void setName(String name) {
  18.                 this.name = name;
  19.         }

  20.         public int getAge() {
  21.                 return age;
  22.         }

  23.         public void setAge(int age) {
  24.                 this.age = age;
  25.         }
  26.        
  27. }
复制代码
看蒙了


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马