ListIterator和并发修改异常的问题
我有两个 ArrayList,每个都保存一定大小的块:blockList、eraserList。块是具有两个字段的对象:开始和结束。我需要从另一组块中减去一组块。
我必须遍历eraserList并从它们重叠的blockList中“擦除”块。因此我的代码看起来像:
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.
...
}
}
}
我不明白为什么我会收到并发修改异常。我从不修改eraserList。
我正在尝试修改“Block block = it.next();”中分配的块对象陈述。我还通过删除或添加块到列表来修改块列表。我认为 ListIterator 的全部意义在于它允许您修改、添加或减去您正在浏览的列表。
失败跟踪指向块擦除器 = it.next();作为绘制例外的线条,但我不知道那告诉我什么。
谁能帮我弄清楚我做错了什么?
谢谢!
I have two ArrayLists, each holding blocks of certain size: blockList, eraserList. Blocks are objects with two fields: start and end. I need to subtract one set of blocks from the other set of blocks.
I must walk through the eraserList and "erase" blocks out of the blockList where they overlap. Thus my code looks like:
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.
...
}
}
}
I don't understand why I am getting a Concurrent Modification Exception. I never modify the eraserList.
I am trying to modify the block object that is assigned in the "Block block = it.next();" statement. I am also modifying the blockList by removing or adding blocks to the list. I thought the whole point of the ListIterator was that it allowed you to modify, add, or subtract a list you are walking through.
The Failure Trace points to the Blocks eraser = it.next(); as the line drawing the exception, but I don't know what that is telling me.
Can anyone help me figure out what I am doing wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,ListIterator 被设计为允许修改列表。但是您没有使用ListIterator的remove()方法,而是直接操作底层列表本身。
Yes, ListIterator is designed to allow the modification of the list. But you are not using the remove() method of the ListIterator, but directly manipulate the underlying list itself.
替换
为
如果您以其他方式删除元素,您可以获得 CME。
Replace
with
If you remove an element another way, you can get a CME.
您需要在
Iterator
上调用remove()
,而不是在List
上。来自javadoc:
You need to call
remove()
on theIterator
, not on theList
.From the javadoc: