递归检索列表中的所有元素

时间:2019-01-11 10:15:12

标签: java list recursion

我有一个这样定义的“工作”类:

public class Job extends AbstractJob
{
    private String name;
    private String jobCount;
    private String status;
    private List<Job> children;


    public Job(String name, String jobCount, String status, List<Job> children) {
        this.name = name;
        this.jobCount = jobCount;
        this.status = status;
        this.children = children;
    }

    public String getName()
    {
        return name;
    }

    public String getJobCount()
    {
        return jobCount;
    }

    public String getStatus()
    {
        return status;
    }

    public List<Job> getChildren() {
        return children;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setJobCount(String jobCount) {
        this.jobCount = jobCount;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setChildren(List<Job> children) {
        this.children = children;
    }
}

我想做的是能够检索工作列表中的所有元素,包括他们的孩子。到目前为止,这是我所做的:

   public List<Job> getJobChildren(Job job) {

        List<Job> result = new ArrayList<Job>();
        if (job == null) {
            return new ArrayList<Job>();
        }
        List<Job> children = job.getChildren();
            for (Job k : children) {
                if (children != null && !children.isEmpty()) {
                    result.addAll(children);
                    getJobChildren(k);
                } else {
                    result.add(k);
                }
            }
            return result;
    }

在主类中,我实例化并填充了作业,以便对其进行测试,但是我得到了nullpointerException:

Job job2 = new Job("JOB0002","0002","Finished",null);
Job job3 = new Job("JOB0003","0003","Error",jobSubList);
Job job4 = new Job("JOB0004","0004","En cours",null);
jobSubList.add(job4);
List<Job> jobList = new ArrayList<Job>();
jobList.add(job2);
jobList.add(job3);
Job job = new Job("JOB0001","0001","En Cours",jobList);

我知道为什么会收到异常,但是我无法编辑该方法以使其返回所有子项。

想法是遍历所有工作,看看他们是否也有自己的工作。如果他们愿意,我会更深入,并找到所有这些孩子,直到我找到所有工作为止,我都会继续这样做。

能否请您告诉我该方法做错了什么?

谢谢。

2 个答案:

答案 0 :(得分:3)

NullPointerException是因为您正在for循环开始后对children列表进行空检查。应该在进入循环之前 完成。另外,我注意到您没有汇总结果,在每次getJobChildren()调用时,您都会实例化一个新列表,并且在方法返回时,没有将其添加到父调用中。

用于遍历子级列表的深度优先递归算法(假设没有循环)可以如下:

public List<Job> getJobChildren(final Job job, final List<Job> result) {
    if (job == null) {
        return result;
    }

    result.add(job);
    if(job.getChildren() != null){
        for(Job current : job.getChildren()){
            getJobChildren(current, result);
        }
    }

    return result;
}

您将需要使用新的ArrayList触发第一个调用以收集结果。

List<Job> results = new ArrayList<>();
getJobChildren(parentJob, results);

// Use the results here.

答案 1 :(得分:1)

您需要检查是否为空:

    if (job.getChildren() != null) {
        for (Job k : children) {

job.getChildren()可以为null。无需迭代空列表。

正确的方法:

public static List<Job> getJobChildren(Job job) {

    List<Job> result = new ArrayList<Job>();
    if (job == null) {
        return new ArrayList<Job>();
    }
    List<Job> children = job.getChildren();
    if (job.getChildren() != null) {
        for (Job k : children) {
            if (children != null && !children.isEmpty()) {
                result.addAll(children);
                getJobChildren(k);
            } else {
                result.add(k);
            }
        }
    }
    return result;
}
相关问题