在这种情况下如何避免ConcurrentModificationException?

问题描述 投票:0回答:1

[我了解,当我尝试修改(在这种情况下添加)列表时,我得到了ConcurrentModificationException,但是解决此问题的最佳解决方案是什么?

for (Map.Entry<String, Child> entry : children
                .entrySet(){

             childEvent.child = entry.getValue();

                    if (childEvent.getDate() != null
                            && childEvent.getDate().equals(selectedDate)) {


if(this.selectedDayevents.isEmpty()) {

    // List                                
    this.selectedDayevents.add(childEvent);
                        }

for (CareDay selectedCareDay : this.selectedDayevents) {
    // Here I have to combine data in some cases...
}


}
java list for-loop concurrentmodification
1个回答
-1
投票

您可以参考以下link,它提供了处理不同集合类型上的异常的方法。您可以使用Iterator代替for循环

© www.soinside.com 2019 - 2024. All rights reserved.