lyn4215 于 2016.04.28 18:28 提问

c：froeach 遍历两层list

4个回答

CSDNXIAON   2016.04.28 18:32

c:forEach中对List的遍历
----------------------同志你好，我是CSDN问答机器人小N，奉组织之命为你提供参考答案，编程尚未成功，同志仍需努力！

liuyihuansmm   2016.04.28 21:24

``````    public class Run {

public static void main(String[] args) {
Child c1 = new Child(1);
Child c2 = new Child(2);
Child c3= new Child(3);
Child c4 = new Child(4);
Child c5 = new Child(5);
Child c6 = new Child(6);

List<Child> inner1 = new ArrayList<Child>();
inner1.add(c1);
inner1.add(c2);

List<Child> inner2= new ArrayList<Child>();
inner1.add(c3);
inner1.add(c4);

List<Child> inner3 = new ArrayList<Child>();
inner1.add(c5);
inner1.add(c6);

List<List<Child>> out = new ArrayList<List<Child>>();
out.add(inner1);
out.add(inner2);
out.add(inner3);

for(List<Child> inner:out) {
for(Child temp: inner){
System.out.println(temp);
}
}

}

}
``````

lyn4215 蟹蟹。

lyn4215   2016.04.28 18:38

liuyihuansmm   2016.04.28 21:25

``````    public class Run {

public static void main(String[] args) {
Child c1 = new Child(1);
Child c2 = new Child(2);
Child c3= new Child(3);
Child c4 = new Child(4);
Child c5 = new Child(5);
Child c6 = new Child(6);

List<Child> inner1 = new ArrayList<Child>();
inner1.add(c1);
inner1.add(c2);

List<Child> inner2= new ArrayList<Child>();
inner1.add(c3);
inner1.add(c4);

List<Child> inner3 = new ArrayList<Child>();
inner1.add(c5);
inner1.add(c6);

List<List<Child>> out = new ArrayList<List<Child>>();
out.add(inner1);
out.add(inner2);
out.add(inner3);

for(List<Child> inner:out) {
for(Child temp: inner){
System.out.println(temp);
}
}

}

}
``````