请原谅我,因为我是编码方面的新手。我已经尝试研究将一些丢失的记录添加到列表中的方法,但似乎仍然无法将其正确地放入我的代码中。
我有两个具有不同结果集的 ArrayList。比如说,第一个是在其他方法中派生的并存储在 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。
最佳答案
创建一个本地列表并向其中添加缺失的记录,然后将本地列表中的所有元素添加到 abcList
List<TAbcModel> temp = new ArrayList<>();
在你的循环中:
} else {
temp.add(abc);
}
循环后
abcList.addAll(temp);
关于java - 如果缺少的记录不在数组列表中,则将其添加到数组列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56357397/