如何在java中的列表中迭代列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25439409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate a list inside a list in java?
提问by user2142786
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;
}
}
second is for children
第二个是给孩子的
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());
}
}
}
回答by Rod_Algonquin
You can use the advance forloop
for iterating (which is equals to using iterator
) child ArrayList and parent ArrayList
您可以使用advance forloop
for 迭代(相当于使用iterator
)子 ArrayList 和父 ArrayList
sample:
样本:
for(Father f : fatherList)
{
for(Children c : f.getChildrens)
{
}
}
回答by 75inchpianist
Iterator<Father> iterator = fatherList.iterator();
while (iterator.hasNext()) {
Father father = iterator.next();
Iterator<Children> childiter = father.getChildren().iterator();
while(childiter.hasNext()){
System.out.println(childiter.next().toString());
}
}
回答by Mohan Raj B
Simple use two for loops
简单使用两个 for 循环
for (Father f : fatherlist) {
for (Children c : f.getChildrens) {
}
}
回答by Loganathan Mohanraj
Override toString() in Father and Children. Your toString() implementation of Father should use children.toString() to build the resultant string and that is it. Then printing the father will print the details of father and children both.
覆盖父和子中的 toString()。您对父的 toString() 实现应该使用 children.toString() 来构建结果字符串,就是这样。然后打印父亲将打印父亲和孩子的详细信息。
Children implementation of toString()
toString() 的子级实现
public String toString() {
StringBuffer buff = new StringBuffer();
buff.append("[Name : ");
buff.append(this.name).append(", Age : ");
buff.append(this.age);
buff.append("]");
return buff.toString();
}
Father implementation of toString()
toString() 的父实现
@Override
public String toString() {
StringBuffer buff = new StringBuffer();
buff.append("[ Father Name : ");
buff.append(this.name);
buff.append(", Age : ");
buff.append(this.age);
buff.append(", Childrens : { ");
for (Children children : getChildrens()) {
buff.append(children);
buff.append(" ");
}
buff.append("}");
return buff.toString();
}
Then printing Father will print the information about Father and Children both.
然后打印Father 将打印有关Father 和Children 的信息。
System.out.println(father);
回答by Marc Baumbach
You can use a nested for
loop to accomplish this. Here's an example:
您可以使用嵌套for
循环来完成此操作。下面是一个例子:
for (Father f : fatherlist) {
System.out.println("Father: " + f.getName());
System.out.println("Children:");
for (Children c : f.getChildrens()) {
System.out.println(c.getName());
}
}
Using the Iterator
approach, you would accomplish it this way:
使用该Iterator
方法,您可以通过以下方式完成:
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());
}
}
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.
作为一般风格的建议,我建议重命名Children
类Child
和命名方法getChildrens
,并setChildrens
在Father
对getChildren
和setChildren
分别。
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.
我什至建议更进一步并删除该setChildren
方法并提供一种addChild(Child child)
方法,以便您可以控制List
包含子项的 。这样做的一个好处是,您可以保证 aList
被实例化,以便您定义的这些循环NullPointerException
在没有子项添加到特定Father
实例的情况下不会命中 a 。
回答by Shishir Kumar
Just to print the objects, you may use below code snippet
只是为了打印对象,您可以使用下面的代码片段
for (Father father2 : fatherList) {
System.out.print("Father: "+ReflectionToStringBuilder.toString(father2));
for (Children children2 : childrens) {
System.out.print(" Children: " + ReflectionToStringBuilder.toString(children2));
}
System.out.println();
}
回答by Rajesh D
Iterating over List using Java 8 forEach.
使用 Java 8 forEach 迭代 List。
{
List<Father> fatherList = new ArrayList<Father>();
// Create Father Object
Father father = new Father();
father.setName("john");
father.setAge(25);
List<Children> childrens = new ArrayList<Children>();
// Create child object
Children children = new Children();
children.setName("david");
children.setAge(2);
childrens.add(children);
father.setChildrens(childrens);
fatherList.add(father);
fatherList.forEach(f -> {
System.out.println("Father's Name : " + f.getName());
f.getChildrens().forEach(c -> {
System.out.println("Children's Name : " + c.getName());
});
});
}