For 循环中的 java.util.ConcurrentModificationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19702461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:31:10  来源:igfitidea点击:

java.util.ConcurrentModificationException in For loop

javafor-loopconcurrentmodification

提问by EsmaeelQash

I am trying to program an IM software, I want to let user leave the conversation and tell his partner that he has left... I prefer to use for loop instead Iterator, seek all the users and get the user who ask to leave and remove him... like that:

我正在尝试编写一个 IM 软件,我想让用户离开对话并告诉他的伙伴他已经离开了......我更喜欢使用 for 循环而不是迭代器,寻找所有用户并让要求离开的用户和删除他......就像这样:

   for(Clientuser Cu: EIQserver.OnlineusersList)
          if(Cu.ID.equals(thsisUser.ID)) // find the user who ask to leave 
          {
          Omsg.setBody("@@!&$$$$@@@####$$$$"); //code means : clien! ur parter leaves...
                 sendMessage(Omsg); // sed message to thje partner with that code
                 EIQserver.OnlineusersList.remove(Cu);// remove the partner
                EIQserver.COUNTER--;// decrease counter.

          }

I get Exception: java.util.ConcurrentModificationException

我得到异常:java.util.ConcurrentModificationException

I was using iterators, and to get rid of this exception, I convert to for, but the same exception still appears!! how may I get rid of this exception?

我正在使用迭代器,为了摆脱这个异常,我转换为 for,但仍然出现相同的异常!!我怎样才能摆脱这个例外?

采纳答案by Konstantin Yovkov

Use Iteratorinstead of looping. For example:

使用迭代器而不是循环。例如:

Iterator<Clientuser> iterator = EIQserver.OnlineusersList.iterator();
while (iterator.hasNext()) {
    Clientuser next = iterator.next();
    if(next.ID.equals(thsisUser.ID)) {
        Omsg.setBody("@@!&$$$$@@@####$$$$"); 
        sendMessage(Omsg); 
        iterator.remove();// remove the partner
    }
}

回答by afk5min

Faulting line: EIQserver.OnlineusersList.remove(Cu);

故障线路: EIQserver.OnlineusersList.remove(Cu);

You can only remove elements from a collection that is being iterated over via the Iteratorobject you are using to iterate.

您只能从正在通过Iterator您用于迭代的对象迭代的集合中删除元素。

for (Iterator<Clientuser> it = EIQserver.OnlineusersList.iterator(); it.hasNext();)
{
    Clientuser cu = it.next();
    if (!cu.ID.equals(thsisUser.ID))
        continue;
    // other code
    it.remove();
}

回答by Debabrata

As you are Iterating the EIQserver collection class, you can not remove element from the same class. Use a different collection for Iteration and remove the element from EIQserver class.

当您迭代 EIQserver 集合类时,您不能从同一个类中删除元素。使用不同的迭代集合并从 EIQserver 类中删除元素。

 List temp = ListofEIQserverobject;
  for(Clientuser Cu: temp.OnlineusersList){
    ..... your code then 
      EIQserver.OnlineusersList.remove(Cu);
    }

回答by Trick

One possible solution is also to transform the Collection to HashMap, save id's to remove and then remove it from the HashMap.

一种可能的解决方案也是将集合转换为 HashMap,保存要删除的 id,然后将其从 HashMap 中删除。

Collection<Integer> removeIds = new ArrayList<Integer>();
Map<Integer,ClientUser> all = new HashMap<Integer,ClientUser>();

for(Clientuser Cu: EIQserver.OnlineusersList) {
all.put(cu.ID,Cu);
      if(Cu.ID.equals(thsisUser.ID)) // find the user who ask to leave 
      {
      Omsg.setBody("@@!&$$$$@@@####$$$$"); //code means : clien! ur parter leaves...
             sendMessage(Omsg); // sed message to thje partner with that code
            EIQserver.COUNTER--;// decrease counter.
    removeIds.add(Cu.ID);

      }
}

回答by SeniorJD

Use Iteratorfor do something with list in loop:

使用Iterator用于做一些与环路列表:

Iterator<Clientuser> iter = EIQserver.OnlineuserList.iterator();
for(;iter.hasNext();) {
    Clientuser Cu = iterator.next();
    if(Cu.ID.equals(thsisUser.ID)) {
        Omsg.setBody("@@!&$$$$@@@####$$$$"); 
        sendMessage(Omsg); 
        iterator.remove(next);
    }
}