ListIterator和并发修改异常的问题

发布于 2025-01-08 20:42:58 字数 1284 浏览 1 评论 0原文

我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2025-01-15 20:42:58

是的,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.

难得心□动 2025-01-15 20:42:58

替换

blockList.remove(block);

it.remove();

如果您以其他方式删除元素,您可以获得 CME。

Replace

blockList.remove(block);

with

it.remove();

If you remove an element another way, you can get a CME.

亽野灬性zι浪 2025-01-15 20:42:58

您需要在 Iterator 上调用 remove(),而不是在 List 上。

来自javadoc:

如果单个线程发出一系列方法调用
违反了对象的契约,该对象可能会抛出此异常
例外。例如,如果一个线程直接修改一个集合
当它使用快速失败迭代器迭代集合时,
迭代器将抛出此异常。

You need to call remove() on the Iterator, not on the List.

From the javadoc:

If a single thread issues a sequence of method invocations that
violates the contract of an object, the object may throw this
exception. For example, if a thread modifies a collection directly
while it is iterating over the collection with a fail-fast iterator,
the iterator will thow this exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文