ListIterator 和并发修改异常的问题 [英] Problems with ListIterator and Concurrent Modification Exception

查看:32
本文介绍了ListIterator 和并发修改异常的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我正在尝试修改在Block block = it.next();"中分配的块对象陈述.我还通过在列表中删除或添加块来修改 blockList.我认为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆