如何迭代java中列表中的列表? [英] How to iterate a list inside a list in java?
问题描述
您好我有两个值对象类。
Hi i have two value object classes .
package org.array;
import java.util.List;
public class Father {
private String name;
private int age ;
private List<Children> Childrens;
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;
}
public List<Children> getChildrens() {
return Childrens;
}
public void setChildrens(List<Children> childrens) {
Childrens = childrens;
}
}
秒是孩子
package org.array;
public class Children {
private String name;
private int 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;
}
}
我想要打印那里的值我嵌套在列表中的列表这里我只在对象内部放置一个值而在实际中我有很多值。所以我在父亲名单中嵌套儿童名单。我怎样才能打印或获得孩子和父亲的价值。这是我的逻辑。
and i want to print there value i nested a list inside a list here i am putting only a single value inside the objects while in real i have many values . so i am nesting list of children inside father list. how can i print or get the value of child and father both. here is my logic.
package org.array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayDemo {
public static void main(String[] args) {
List <Father> fatherList = new ArrayList<Father>();
Father father = new Father();
father.setName("john");
father.setAge(25);
fatherList.add(father);
List <Children> childrens = new ArrayList<Children>();
Children children = new Children();
children.setName("david");
children.setAge(2);
childrens.add(children);
father.setChildrens(childrens);
fatherList.add(father);
Iterator<Father> iterator = fatherList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.toString());
}
}
}
推荐答案
您可以使用嵌套的 for
循环来完成此任务。这是一个例子:
You can use a nested for
loop to accomplish this. Here's an example:
for (Father f : fatherlist) {
System.out.println("Father: " + f.getName());
System.out.println("Children:");
for (Children c : f.getChildrens()) {
System.out.println(c.getName());
}
}
使用 Iterator
方法,你可以这样做:
Using the Iterator
approach, you would accomplish it this way:
Iterator<Father> i = fatherList.iterator();
while (i.hasNext()) {
Father f = i.next();
System.out.println("Father: " + f.getName());
System.out.println("Children:");
Iterator<Children> ci = f.getChildrens().iterator();
while (ci.hasNext()) {
Children c = ci.next();
System.out.println(c.getName());
}
}
作为一般风格的建议,我建议重命名儿童
课程儿童
并重命名方法 getChildrens
和 setChildrens
在父
到 getChildren
和 setChildren
分别。
As a general style suggestion, I would suggest renaming the Children
class to Child
and rename the methods getChildrens
and setChildrens
in Father
to getChildren
and setChildren
respectively.
我甚至建议更进一步删除 setChildren
方法并提供 addChild(子项)
方法,以便您可以控制包含子项的列表
。这样做的一个好处是你可以保证实例化 List
,这样你定义的这些循环就不会遇到 NullPointerException
如果没有子项被添加到特定的父
实例。
I would even suggest taking it a step further and remove the setChildren
method and provide an addChild(Child child)
method such that you have control over the List
that contains the children. A benefit to this is that you can guarantee a List
is instantiated such that these loops you are defining won't hit a NullPointerException
in the case that no children were added to a particular Father
instance.
这篇关于如何迭代java中列表中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!