亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

遞歸檢索列表中的所有元素

遞歸檢索列表中的所有元素

森林海 2022-04-28 16:00:55
我有一個這樣定義的“工作”類: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;    }在主類中,我實例化并填充了 so 作業來測試它,但我得到了一個 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 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

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.


查看完整回答
反對 回復 2022-04-28
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

您需要檢查空值:


    if (job.getChildren() != null) {

        for (Job k : children) {

job.getChildren() 可以為空。無需迭代空列表。


修正方法:


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;

}


查看完整回答
反對 回復 2022-04-28
  • 2 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號