遍历列表的列表

huangapple go评论57阅读模式
英文:

Iterating over List of Lists

问题

我尝试遍历一个列表的列表,但我一直在不断地遇到并发修改异常(ConcurrentModificationException),即使在遍历列表时使用了迭代器来删除和添加元素。

我在社区中搜索了类似的问题,但我找到的那些并没有帮助我。真希望你们能帮我找出如何做我需要做的事情。

我有一个 `ListIterator<List<Event<T>>> itrListsEvent = partitionSubLists.listIterator();`
`partitionSubLists` 是一个 **列表的列表**。所以我有**一个更大的列表**,里面有**四个子列表**。

我需要遍历这些子列表,在遍历时我需要删除和添加元素。在完成对第一个子列表的迭代后,我需要继续迭代到第二个子列表,依此类推。

以下是我到目前为止所做的:

```java
public List<List<Event<T>>> partitionedLists(List<Event<T>> list) {     
    int listSize = list.size();
    int partitionSize = listSize / 4;
               
    List<List<Event<T>>> partitions = new ArrayList<>();

    for (int i = 0; i < listSize; i += partitionSize) {
        partitions.add(list.subList(i, Math.min(i + partitionSize, list.size())));
    }        
    return partitions;        
}

List<List<Event<T>>> partitionSubLists = partitionedLists(List<Event<T>>);
ListIterator<List<Event<T>>> itrListsEvent = partitionSubLists.listIterator();

while(itrListsEvent.hasNext()) {
    List<Event<T>> listPE = new ArrayList<Event<T>>();           	
    listPE = itrListsPrefixEvent.next(); 
    ListIterator<Event<T>> itrEvent = listPE.listIterator();

    while(itrEvent.hasNext()) {
        // 在这里,我在子列表内部删除和添加元素。
        // 完成后,我需要回到外部的第一个 while 循环,继续前进到下一个子列表
        // 在这时,我遇到了并发修改异常(ConcurrentModificationException)

        itrEvent.remove();
        // 其他代码
        itrEvent.add(new Event<T>());
    }
}

请注意,我只翻译了您提供的代码部分,不包括代码外的任何内容。如果您有更多问题或需要进一步的帮助,请随时提问。

<details>
<summary>英文:</summary>

I&#39;m trying to iterate over a list os lists but I&#39;m getting CME all the time even using Iterator to remove and add elements while iterating over the lists. 

I searched here in the community for similar questions but those I found didn&#39;t help me. Really hope you guys help me to figure out how to do what I need to do.

I have I `ListIterator&lt;List&lt;Event&lt;T&gt;&gt;&gt; itrListsEvent = partitionSubLists.listIterator();`
`partitionSubLists` is A `list of lists`. So I have **one bigger List** and inside it I have **four sublists.**

I need to iterate over the sublists, and while iterating I remove and add elements. After finishing to iterate over the first sublist, I need to go forward to iterate over the second sublist and so on and so forth.

This is what I&#39;ve done so far:

      public List&lt;List&lt;Event&lt;T&gt;&gt;&gt; partitionedLists (List&lt;Event&lt;T&gt;&gt; list)
    	{     
        	int listSize = list.size();
            int partitionSize = listSize / 4;
                   
            List&lt;List&lt;Event&lt;T&gt;&gt;&gt; partitions = new ArrayList&lt;&gt;();
    
            for (int i = 0; i &lt; listSize; i += partitionSize) 
            {
                partitions.add(list.subList(i, Math.min(i + partitionSize, list.size())));
            }        
            return partitions;        
    	}

    List&lt;List&lt;Event&lt;T&gt;&gt;&gt; partitionSubLists     = partitionedLists(List&lt;Event&lt;T&gt;&gt;);
    ListIterator&lt;List&lt;Event&lt;T&gt;&gt;&gt; itrListsEvent = partitionSubLists.listIterator();
    
    while(itrListsEvent.hasNext())
    {
           List&lt;PrefixEvent&lt;T&gt;&gt; listPE 	= new ArrayList&lt;Event&lt;T&gt;&gt;();           	
           listPE 	= itrListsPrefixEvent.next(); 
           ListIterator&lt;Event&lt;T&gt;&gt; itrEvent = listPE.listIterator();
    
           while(itrEvent.hasNext())
           {
              //here I remove and add elements inside the sublist.
              //when finished, I need to go back to first while and go forward to the next sublists
              //and in this moment, i got ConcurrentModificationException
    
             itrEvent.remove()
                .
                . 
                .
            // some code here
    
             itrEvent.add(new Event&lt;T&gt;);
           }
    
    }



</details>


# 答案1
**得分**: 1

这似乎不太清楚你究竟想要实现什么。据我理解,你可以像这样实现:

```java
List<PrefixEvent<T>> listPE = itrListsPrefixEvent.next();
// 没有使用迭代器。

for (int i = 0; i < listPE.size(); ++i) {
  listPE.remove(i);

  // 在这里插入一些代码

  listPE.add(i, new Event<>());
}

这样做可以避免ConcurrentModificationException,因为你在创建迭代器后没有在结构上修改列表。

如果实际上你在itrEvent.remove()itrEvent.add(new Event<T>())之间并不需要“一个元素被移除”的列表,你可以继续使用ListIterator,然后将值设置为新值:

itrEvent.set(new Event<>());
英文:

It's rather unclear exactly what you're trying to achieve. As far as I understand, you could achieve it like this:

List&lt;PrefixEvent&lt;T&gt;&gt;  listPE   = itrListsPrefixEvent.next();
// No iterator.

for (int i = 0; i &lt; listPE.size(); ++i) {
  listPE.remove(i);

  // some code here

  listPE.add(i, new Event&lt;&gt;());
}

This avoids a ConcurrentModificationException because you don't structurally modify the list after creating an Iterator.

If you don't actually require the "one element removed" list in between the itrEvent.remove() and itrEvent.add(new Event&lt;T&gt;()), you can continue to use the ListIterator, and then set the value to a new value:

itrEvent.set(new Event&lt;&gt;());

huangapple
  • 本文由 发表于 2020年4月8日 02:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61087282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定