如何在java中的列表中迭代列表

如何在java中的列表中迭代列表

本文介绍了如何在java中的列表中迭代列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个值对象类.

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());
    }
}

作为一般风格建议,我建议将 Children 类重命名为 Child 并重命名方法 getChildrenssetChildrens<Father 中的/code> 分别为 getChildrensetChildren.

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(Child child) 方法,以便您可以控制 包含子项的列表.这样做的一个好处是你可以保证一个 List 被实例化,这样你定义的这些循环在没有孩子被添加到一个的情况下不会遇到 NullPointerException特定的 Father 实例.

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中的列表中迭代列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 14:09