- import java.util.ArrayList;
- public class ArrayListDemo {
-
- public static void main(String[] args) {
-
- /**
- * 使用集合存储多个班级的集合,然后每一个班级集合的元素应该是学生对象
- */
- ArrayList<ArrayList<Student>> al = new ArrayList<ArrayList<Student>>() ;
-
- // 创建基本班集合对象
- ArrayList<Student> jcAl = new ArrayList<Student>() ;
-
- // 添加元素
- jcAl.add(new Student("张三1" , 23)) ;
- jcAl.add(new Student("李四1" , 24)) ;
- jcAl.add(new Student("王五1" , 25)) ;
- jcAl.add(new Student("赵六1" , 26)) ;
-
- // 把jcAl添加到al中
- al.add(jcAl) ;
-
- // 创建UI班集合对象
- ArrayList<Student> uiAl = new ArrayList<Student>() ;
-
- // 添加元素
- uiAl.add(new Student("张三" , 23)) ;
- uiAl.add(new Student("李四" , 24)) ;
- uiAl.add(new Student("王五" , 25)) ;
- uiAl.add(new Student("赵六" , 26)) ;
-
- // 把uiAl添加到al中
- al.add(uiAl) ;
-
- // 遍历
- // ArrayList<ArrayList<Student>> al = new ArrayList<ArrayList<Student>>() ;
- for(ArrayList<Student> a : al){
- for(Student s : a){
- System.out.println(s.getName() + "---" + s.getAge());
- }
- System.out.println();
- }
- }
- }
复制代码- public class Student {
- private String name ;
-
- private int age ;
- public Student() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Student(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
-
- }
复制代码 看蒙了
|
|