ListIterator和并发修改异常的问题

MyT*_*der 3 java iterator arraylist

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

我必须遍历好橡皮擦列表和“擦除”块,使其脱离重叠的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.
                        ...
                  }
            }
        }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会收到并发修改异常。我从不修改橡皮擦列表。

我试图修改在“块块= it.next();”中分配的块对象。声明。我还通过删除块或将块添加到列表中来修改blockList。我认为ListIterator的全部要点是它允许您修改,添加或减去正在遍历的列表。

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

谁能帮助我找出我做错了什么?

谢谢!

Jan*_*Jan 6

是的,ListIterator旨在允许修改列表。但是,您不是在使用ListIterator的remove()方法,而是直接操纵基础列表本身。