gpt4 book ai didi

java - ListIterator 和并发修改异常的问题

转载 作者:行者123 更新时间:2023-12-04 23:27:58 25 4
gpt4 key购买 nike

我有两个 ArrayList,每个都包含一定大小的块:blockList、eraseList。块是具有两个字段的对象:开始和结束。我需要从另一组块中减去一组块。

我必须遍历删除器列表并从它们重叠的块列表中“删除”块。因此我的代码看起来像:

 void eraseBlocks (Arrylist<Blocks> blockList, ArrayList<Blocks> eraserList) {
ListIterator<Blocks> it = blockList.listIterator();

for (Blocks eraser: eraserList) {
while (it.hasNext()) {
Blocks block= it.next();
if ((eraser.start <= block.start) && (eraser.end >= block.end))
blockList.remove(block);
else if ((eraser.start <= block.start) && (eraser.end < block.end)){
block.set(start, eraser.end);
else if () {
...
//more code for where the eraser partially erases the beginning, end, or splits the block
//if statements call the .add(), .set(), and remove() methods on the blockList.
...
}
}
}

我不明白为什么我会收到并发修改异常。我从不修改删除器列表。

我正在尝试修改在“Block block = it.next();”中分配的块对象陈述。我还通过在列表中删除或添加块来修改 blockList。我认为 ListIterator 的全部意义在于它允许您修改、添加或减去您正在浏览的列表。

故障跟踪指向块删除器 = it.next();作为绘制异常的线条,但我不知道那是在告诉我什么。

谁能帮我弄清楚我做错了什么?

谢谢!

最佳答案

是的,ListIterator 旨在允许修改列表。但是您没有使用 ListIterator 的 remove() 方法,而是直接操作底层列表本身。

关于java - ListIterator 和并发修改异常的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9421411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com