遍历嵌套集合

package niu.cheng5;


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


//遍历嵌套集合


public class ArrayListTest {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList> a=new ArrayList>();
ArrayList a2=new ArrayList();
ArrayList a3=new ArrayList();
ArrayList a4=new ArrayList();
Student s1=new Student("axx",10);
Student s2=new Student("bxx",11);
Student s3=new Student("cxx",12);
Student s4=new Student("dxx",13);
Student s5=new Student("exx",11);
Student s6=new Student("fxx",12);
Student s7=new Student("gxx",13);
Student s8=new Student("hxx",11);
Student s9=new Student("ixx",12);
Student s10=new Student("jxx",13);

a2.add(s1);
a2.add(s2);
a2.add(s3);
a2.add(s4);
a3.add(s5);
a3.add(s6);
a3.add(s7);
a4.add(s8);
a4.add(s9);
a4.add(s10);

a.add(a2);
a.add(a3);
a.add(a4);
for(ArrayList x:a){
/*
//方式1
for(int n=0;n Student s=x.get(n);
System.out.println(s.getName()+"--"+s.getAge());
}
*/

/*
//方式2
for(Student y:x){
System.out.println(y.getName()+"--"+y.getAge());

}
*/

//方式3
Iterator l=x.iterator();
while(l.hasNext()){
Student s=l.next();
System.out.println(s.getName()+"--"+s.getAge());
}

}

}


}

你可能感兴趣的:(学习记录)