递归遍历java类的列表

递归遍历java类的列表,java,list,Java,List,我有一个类版本,它包含自己的list对象,现在我希望递归列表得到解析,并希望它在一个列表中 这是我的班级: public class OnTimeNowRelease implements Serializable { /** * */ private static final long serialVersionUID = 1L; int id; String name; String can_modify; Strin

我有一个类版本,它包含自己的list对象,现在我希望递归列表得到解析,并希望它在一个列表中

这是我的班级:

public class OnTimeNowRelease implements Serializable
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    int id;
    String name;
    String can_modify;
    String start_date;
    String due_date;
    String velocity_start_date;
    String release_notes;
    String status;
    String is_active;
    String release_type;
    List<OnTimeNowRelease> children ; 
    getter setter//
}
public类OnTimeNowRelease实现了可序列化
{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
int-id;
字符串名;
字符串可以修改;
字符串开始日期;
字符串到期日;
字符串速度\开始\日期;
字符串释放注释;
字符串状态;
字符串处于活动状态;
字符串释放类型;
列出儿童名单;
吸气剂设定器//
}

如何遍历子项的
列表
,直至第n级?它就像遍历树。如果对象没有子对象,它的值是
children=null

我不知道你到底想要什么:把树中的所有项目都列出来打印它?将一个项目的子项移动到其父项

在第一种情况下,您可以向获得它的类添加一个补偿函数:

public List<OnTimeNowRelease> obtainDescendants() {
    // create a list for the childs
    List<OnTimeNowRelease> items = new ArrayList<OnTimeNowRelease>();
    // add myself
    items.addAll(this);

    if(children != null) {
        // add my childs and his childs to the list
        for(OnTimeNowRelease child : this.children) {       
            items.addAll( child.obtainDescendants() );
        }
    }

    return items;
}

这是一个简单的遍历示例,您主要在LinkedList或树中看到

public void fetchAllChildren(OnTimeNowRelease root, List<OnTimeNowRelease> childList){
    // if the parent is not defined, nothing to do
    if(root == null){
         return;
    }

    //add the parent to the list. Since java is Reference by Value, the list can be used for recursively adding all the descending elements 
    childList.add(root);
    if(root.children !=null && !root.children.isEmpty()){   
         for(OnTimeNowRelease children : root.children){
                //simple recursive solution add all the children and their children and so on....
                fetchAllChildren(root.children, childList);
         }
    }
}
public void fetchAllChildren(OnTimeNowRelease root,List childList){
//如果未定义父项,则无需执行任何操作
if(root==null){
返回;
}
//将父元素添加到列表中。因为java是按值引用的,所以可以使用该列表递归地添加所有降序元素
添加(根目录);
如果(root.children!=null&&!root.children.isEmpty()){
for(OnTimeNowRelease子项:root.children){
//简单的递归解决方案添加所有子项及其子项等等。。。。
fetchAllChildren(root.children,childList);
}
}
}

您可以尝试以下方法:

public List<OnTimeNowRelease> flattenLists(OnTimeNowRelease obj) {
    List<OnTimeNowRelease> result = new ArrayList<OnTimeNowRelease>();
    List<OnTimeNowRelease> children = obj.getChildren(); 
    if (children==null || children.isEmpty()) {
        return result;
    } else {
        for (OnTimeNowRelease child : children) {
            result.addAll(flattenLists(child));
        }
    }
    return result;
}
公共列表列表(OnTimeNowRelease obj){
列表结果=新建ArrayList();
List children=obj.getChildren();
if(children==null | | children.isEmpty()){
返回结果;
}否则{
for(OnTimeNowRelease子项:子项){
结果.addAll(扁平列表(子));
}
}
返回结果;
}

这将迭代每个
列表的所有子元素
,并递归地将它们的每个子元素添加到一个大列表中。最初只需使用根元素调用它一次

现在我可以遍历到第n级的子级列表
请不要只转储代码。如果你能解释一下你的代码是如何工作的,以及它为什么工作的,那会更有帮助。
public List<OnTimeNowRelease> flattenLists(OnTimeNowRelease obj) {
    List<OnTimeNowRelease> result = new ArrayList<OnTimeNowRelease>();
    List<OnTimeNowRelease> children = obj.getChildren(); 
    if (children==null || children.isEmpty()) {
        return result;
    } else {
        for (OnTimeNowRelease child : children) {
            result.addAll(flattenLists(child));
        }
    }
    return result;
}