首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果缺少的记录不在数组列表中,则将其添加到数组列表中

如果缺少的记录不在数组列表中,则将其添加到数组列表中
EN

Stack Overflow用户
提问于 2019-05-29 17:33:37
回答 1查看 270关注 0票数 4

请原谅我,因为我在编程方面还是个初学者。我尝试过研究如何将一些缺失的记录添加到列表中,但似乎仍然不能正确地将其放入我的代码中。

我有两个具有不同结果集的ArrayLists。比方说,第一个是用其他方法派生的,并存储在abcList中。然后,这个列表在我当前的fixChartStats方法中用作一个参数。

在我的代码中,我将使用从fixChartStats方法中的hql查询派生的第二个列表来检查abcList中的相应记录。

如果记录对应,那么我将执行如下所示的必要操作来更新ApprovedCount编号等,否则将其设置为0。

如何添加我进入第一个数组列表(即abcList)的第二个列表中缺失的记录?这里有没有人能说点什么?如果我的问题不清楚,一定要让我知道。提前谢谢,伙计们!

private void fixChartStats(List<TAbcModel> abcList, Map<String, Object> param, List<IssueModel> issueList, List<DestModel> destList) throws Exception {

    //initialize the hql query
    //translate all fields from Object[] into individual variable

    firstRow = true;
    for (TAbcModel abc : abcList) {
        if (abc.getId().getAbcYear() = abcYear &&
                abc.getId().getAbcMonthId() = abcMonthId &&
                abc.getId().getAbcApplAccnId().getAccnId().equalsIgnoreCase(abcApplAccnId.getAccnId()) {

            if (firstRow) {
                abc.setApprovedCount(abcApprovedCount);
                abc.setCancelledCount(abcCancelledCount);
                firstRow = false;
            } else {
                abc.setApprovedCount(0);
                abc.setCancelledCount(0);
            }
        }else{
            // How to do the necessary here
            // Below is what I've tried
            abcList.add(abc);
        }
    }
}

当我调试时,我注意到它被添加到列表中。但在添加之后不久,ConcurrentModificationException就被抛出了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-29 17:44:20

创建本地列表并向其中添加缺少的记录,然后将本地列表中的所有元素添加到abcList中

List<TAbcModel> temp = new ArrayList<>();

在你的循环中:

} else { 
    temp.add(abc);
}

after循环

abcList.addAll(temp);
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56357397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档