用的ListIterator和并发修改异常问题 [英] Problems with ListIterator and Concurrent Modification Exception
问题描述
我有两个的ArrayList,一定规模的各持股块:阻止列表,eraserList。块是具有两个字段的对象:开始和结束。我需要从另一组块减去一组块。
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.
我必须通过eraserList走路,擦除阻挡了他们重叠的块列表。因此,我的code如下:
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.
...
}
}
}
我不明白为什么我收到一并发修改异常。我从来没有修改eraserList。
I don't understand why I am getting a Concurrent Modification Exception. I never modify the eraserList.
我想修改在分配的块对象一块块= it.next();声明。我也被删除或添加块列表修改阻止列表。我以为的ListIterator整点是,它允许你修改,增加或减去你走在一个列表中。
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.
的失败跟踪指向块橡皮擦= it.next();作为画线外,但我不知道那是什么告诉我。
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?
谢谢!
推荐答案
是的,的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.
这篇关于用的ListIterator和并发修改异常问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!