您好我有两个值对象类。
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;
}
}
并且我想打印那里的值我嵌套在列表中的列表这里我只在对象内部放置一个值而在实际中我有很多值。所以我在父亲名单中嵌套儿童名单。我怎样才能打印或获得孩子和父亲的价值。这是我的逻辑。
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());
}
}
}
答案 0 :(得分:4)
您可以使用嵌套的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());
}
}
使用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());
}
}
作为一般风格建议,我建议将Children
课程重命名为Child
,并将getChildrens
中的方法setChildrens
和Father
重命名为{{分别为1}}和getChildren
。
我甚至建议更进一步,删除setChildren
方法并提供setChildren
方法,以便您可以控制包含子项的addChild(Child child)
。这样做的一个好处是,您可以保证List
被实例化,以便在没有将子项添加到特定List
的情况下,您定义的这些循环不会命中NullPointerException
实例
答案 1 :(得分:1)
您可以使用advance forloop
进行迭代(等于使用iterator
)子ArrayList和父ArrayList
<强>样品:强>
for(Father f : fatherList)
{
for(Children c : f.getChildrens)
{
}
}
答案 2 :(得分:1)
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());
}
}
答案 3 :(得分:1)
在父亲和子女中覆盖toString()。你的父的toString()实现应该使用children.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();
}
父实现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();
}
然后印刷父亲将打印有关父亲和孩子的信息。
System.out.println(father);
答案 4 :(得分:1)
只需打印对象,您可以使用下面的代码段
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();
}
答案 5 :(得分:0)
简单地使用两个for循环
for (Father f : fatherlist) {
for (Children c : f.getChildrens) {
}
}
答案 6 :(得分:0)
使用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());
});
});
}