[英]ConcurrentModificationException When removing element using list iterator java
I have an issue removing the 1st and 2nd element of my list even by using the iterator.即使使用迭代器,我在删除列表的第一个和第二个元素时也遇到问题。
I have read the following threads but can't fix my issue (those were the most relevant but I checked other material as well):我已阅读以下线程,但无法解决我的问题(那些是最相关的,但我也检查了其他材料):
ConcurrentModificationException when trying remove element from list 尝试从列表中删除元素时出现 ConcurrentModificationException
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop 遍历集合,在循环中删除对象时避免 ConcurrentModificationException
So my code looks like this:所以我的代码看起来像这样:
List<List<String>> list = cnf.read();
List<List<String>> nlist = new ArrayList<>();
for (List<String> l : list) {
if (l.size() <= 3) {
nlist.add(l);
} else {
int size = l.size();
while (size > 3) {
List<String> three = l.subList(0, 2);
three.add("Y" + (count++));
//Iterator itr = l.iterator();
ListIterator itr = l.listIterator();
int v = 0;
while (itr.hasNext()) {
itr.next();
if (v == 0 || v == 1) {
itr.remove();
v++;
}
}
l.add(0, "Y" + (count++));
size--;
nlist.add(three);
}
nlist.add(l);
}
}
for (List<String> l : nlist) {
System.out.println(l.toString());
System.out.println(l.size());
}
I get a ConcurrentModificationException at the print statement here :我在此处的打印语句中收到 ConcurrentModificationException :
System.out.println(l.toString());
System.out.println(l.toString());
I tried using iterators for my 2 for loops as well but It doesn't seem to make a difference!我也尝试在我的 2 个 for 循环中使用迭代器,但似乎没什么区别! I am new to posting questions so let me know If I am doing it right!
我是发布问题的新手,所以如果我做对了,请告诉我! Thank you.
谢谢你。
After A long debugging, here is the solution.经过长时间的调试,这里是解决方案。
The sublist function passes by reference and not by value, a sublist created by ArrayList.subList call keeps a reference to the original list and accesses its elementData array directly. sublist 函数通过引用而不是通过值传递,由 ArrayList.subList 调用创建的子列表保留对原始列表的引用并直接访问其 elementData 数组。
For this reason, when adding an element to the "three" list, we alter the state of the original list.出于这个原因,当向“三”列表添加一个元素时,我们改变了原始列表的状态。 this happens here:
这发生在这里:
three.add("Y" + (count++));
三.add("Y" + (count++));
A way of fixing it for this specific case is to create and initialize the "three" list the following way:针对这种特定情况修复它的一种方法是通过以下方式创建和初始化“三个”列表:
String one = l.get(0);
String two = l.get(1);
List<String> three = new ArrayList<>();
three.add(one);
three.add(two);
three.add("Y" + (count));
This allows us to manipulate our lists without getting Concurrency Exceptions (ConcurrentModificationException).这允许我们在不获得并发异常 (ConcurrentModificationException) 的情况下操作我们的列表。 However, if you are manipulating big lists, I would suggest you use another less hardcoded method for list creation.
但是,如果您要操作大列表,我建议您使用另一种不太硬编码的方法来创建列表。
I will mark this thread as answered and hope it helps people.我会将这个帖子标记为已回答,希望对人们有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.