英文:
Iterating over List of Lists
问题
我尝试遍历一个列表的列表,但我一直在不断地遇到并发修改异常(ConcurrentModificationException),即使在遍历列表时使用了迭代器来删除和添加元素。
我在社区中搜索了类似的问题,但我找到的那些并没有帮助我。真希望你们能帮我找出如何做我需要做的事情。
我有一个 `ListIterator<List<Event<T>>> itrListsEvent = partitionSubLists.listIterator();`
`partitionSubLists` 是一个 **列表的列表**。所以我有**一个更大的列表**,里面有**四个子列表**。
我需要遍历这些子列表,在遍历时我需要删除和添加元素。在完成对第一个子列表的迭代后,我需要继续迭代到第二个子列表,依此类推。
以下是我到目前为止所做的:
```java
public List<List<Event<T>>> partitionedLists(List<Event<T>> list) {
int listSize = list.size();
int partitionSize = listSize / 4;
List<List<Event<T>>> partitions = new ArrayList<>();
for (int i = 0; i < listSize; i += partitionSize) {
partitions.add(list.subList(i, Math.min(i + partitionSize, list.size())));
}
return partitions;
}
List<List<Event<T>>> partitionSubLists = partitionedLists(List<Event<T>>);
ListIterator<List<Event<T>>> itrListsEvent = partitionSubLists.listIterator();
while(itrListsEvent.hasNext()) {
List<Event<T>> listPE = new ArrayList<Event<T>>();
listPE = itrListsPrefixEvent.next();
ListIterator<Event<T>> itrEvent = listPE.listIterator();
while(itrEvent.hasNext()) {
// 在这里,我在子列表内部删除和添加元素。
// 完成后,我需要回到外部的第一个 while 循环,继续前进到下一个子列表
// 在这时,我遇到了并发修改异常(ConcurrentModificationException)
itrEvent.remove();
// 其他代码
itrEvent.add(new Event<T>());
}
}
请注意,我只翻译了您提供的代码部分,不包括代码外的任何内容。如果您有更多问题或需要进一步的帮助,请随时提问。
<details>
<summary>英文:</summary>
I'm trying to iterate over a list os lists but I'm getting CME all the time even using Iterator to remove and add elements while iterating over the lists.
I searched here in the community for similar questions but those I found didn't help me. Really hope you guys help me to figure out how to do what I need to do.
I have I `ListIterator<List<Event<T>>> itrListsEvent = partitionSubLists.listIterator();`
`partitionSubLists` is A `list of lists`. So I have **one bigger List** and inside it I have **four sublists.**
I need to iterate over the sublists, and while iterating I remove and add elements. After finishing to iterate over the first sublist, I need to go forward to iterate over the second sublist and so on and so forth.
This is what I've done so far:
public List<List<Event<T>>> partitionedLists (List<Event<T>> list)
{
int listSize = list.size();
int partitionSize = listSize / 4;
List<List<Event<T>>> partitions = new ArrayList<>();
for (int i = 0; i < listSize; i += partitionSize)
{
partitions.add(list.subList(i, Math.min(i + partitionSize, list.size())));
}
return partitions;
}
List<List<Event<T>>> partitionSubLists = partitionedLists(List<Event<T>>);
ListIterator<List<Event<T>>> itrListsEvent = partitionSubLists.listIterator();
while(itrListsEvent.hasNext())
{
List<PrefixEvent<T>> listPE = new ArrayList<Event<T>>();
listPE = itrListsPrefixEvent.next();
ListIterator<Event<T>> itrEvent = listPE.listIterator();
while(itrEvent.hasNext())
{
//here I remove and add elements inside the sublist.
//when finished, I need to go back to first while and go forward to the next sublists
//and in this moment, i got ConcurrentModificationException
itrEvent.remove()
.
.
.
// some code here
itrEvent.add(new Event<T>);
}
}
</details>
# 答案1
**得分**: 1
这似乎不太清楚你究竟想要实现什么。据我理解,你可以像这样实现:
```java
List<PrefixEvent<T>> listPE = itrListsPrefixEvent.next();
// 没有使用迭代器。
for (int i = 0; i < listPE.size(); ++i) {
listPE.remove(i);
// 在这里插入一些代码
listPE.add(i, new Event<>());
}
这样做可以避免ConcurrentModificationException
,因为你在创建迭代器后没有在结构上修改列表。
如果实际上你在itrEvent.remove()
和itrEvent.add(new Event<T>())
之间并不需要“一个元素被移除”的列表,你可以继续使用ListIterator
,然后将值设置为新值:
itrEvent.set(new Event<>());
英文:
It's rather unclear exactly what you're trying to achieve. As far as I understand, you could achieve it like this:
List<PrefixEvent<T>> listPE = itrListsPrefixEvent.next();
// No iterator.
for (int i = 0; i < listPE.size(); ++i) {
listPE.remove(i);
// some code here
listPE.add(i, new Event<>());
}
This avoids a ConcurrentModificationException because you don't structurally modify the list after creating an Iterator.
If you don't actually require the "one element removed" list in between the itrEvent.remove()
and itrEvent.add(new Event<T>())
, you can continue to use the ListIterator
, and then set the value to a new value:
itrEvent.set(new Event<>());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论