简体   繁体   English

从队列中添加/删除时获取 ConcurrentModificationException

[英]Getting ConcurrentModificationException when adding/removing from queue

I have made part of an algorithm below where I need to use a queue, but in Java I can't modify the queue while iterating over it.我在下面需要使用队列的地方做了一部分算法,但是在 Java 中,我无法在迭代队列时修改队列。 I get a ConcurrentModificationException .我得到一个ConcurrentModificationException

What I can I do about this?我能做些什么呢? I've been thinking about this for several hours.我已经考虑了几个小时。

m, n = len(mat), len(mat[0])
visited = [[False] * n for row in range(m)]
q = deque()
destinations = set()

def should_visit(row, col):
    """Return true if (row,col) is a valid position not processed yet"""
    return 0 <= row < m and 0 <= col < n and \
           mat[row][col] != 'o' and not visited[row][col]

for r in range(m):
    for c in range(n):
        if mat[r][c] == 'r':
            q.append((r, c))
            visited[r][c] = True
        elif mat[r][c] == 'b':
            destinations.add((r, c))

dist = 1  # the level in breadth-first-search.
while q:
    for _ in range(len(q)):
        (r, c) = q.popleft()

        for (new_r, new_c) in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):
            if should_visit(new_r, new_c):
                if (new_r, new_c) in destinations:
                    return dist
                q.append((new_r, new_c))
                visited[new_r][new_c] = True
    dist += 1

You get that exception because you use foreach to loop over the queue.你得到这个异常是因为你使用 foreach 循环队列。 foreach's internal iterator will complain about the queue.removeFirst() call. foreach 的内部迭代器会抱怨queue.removeFirst()调用。

Using while (.queue.isEmpty()) { var cell = queue;removeFirst();使用while (.queue.isEmpty()) { var cell = queue;removeFirst(); will work just fine.会工作得很好。 I don't believe that the foreach loop should even be there.我不相信 foreach 循环甚至应该在那里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从列表中添加子列表/从列表中删除子列表时出现java.util.ConcurrentModificationException - java.util.ConcurrentModificationException when adding/removing SubLists from/to lists 从游戏中删除子弹时出现ConcurrentModificationException - ConcurrentModificationException when removing bullet from game 从列表中删除项目时出现 ConcurrentModificationException - ConcurrentModificationException when removing item from a list 获取ConcurrentModificationException,但我没有删除 - Getting a ConcurrentModificationException but I'm not removing 从 ArrayLists 中删除对象时如何避免 concurrentModificationException” - how to avoid concurrentModificationException when removing an object from to ArrayLists" 从ArrayList中删除对象时如何避免ConcurrentModificationException - How to avoid a ConcurrentModificationException when removing objects from an ArrayList 从父实体中删除时,Spring数据JPA @PreRemove ConcurrentModificationException - Spring data JPA @PreRemove ConcurrentModificationException when removing from parent enity 从HashMap中删除元素时发生异常java.util.ConcurrentModificationException - Exception when removing element from HashMap java.util.ConcurrentModificationException 从哈希图中删除元素时出现 java.util.ConcurrentModificationException - java.util.ConcurrentModificationException when removing elements from a hashmap 获取字符串时出现ConcurrentModificationException - ConcurrentModificationException when getting strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM