Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ConcurrentModificationException,但我没有修改我正在迭代的集合_Java_Android_For Loop - Fatal编程技术网

Java ConcurrentModificationException,但我没有修改我正在迭代的集合

Java ConcurrentModificationException,但我没有修改我正在迭代的集合,java,android,for-loop,Java,Android,For Loop,我正在迭代消息对象列表,试图获取所有对话,其中对话意味着在前一条消息之后20分钟内发送了后续消息 第一次迭代运行良好,在第二次迭代中,我得到了一个java.util.ConcurrentModificationException 即使我没有修改消息列表,我也在迭代。请参阅下面的代码 public List<Chat> getAllConversations(int allowedMinutes) { List<Chat> result = new ArrayLis

我正在迭代消息对象列表,试图获取所有对话,其中对话意味着在前一条消息之后20分钟内发送了后续消息

第一次迭代运行良好,在第二次迭代中,我得到了一个java.util.ConcurrentModificationException 即使我没有修改消息列表,我也在迭代。请参阅下面的代码

public List<Chat> getAllConversations(int allowedMinutes) {
    List<Chat> result = new ArrayList<>();
    Chat currentConvo = new Chat();
    List<Message> ml = this.getMessageList();
    for (Message m : ml) {
        if (currentConvo.getMessageList().isEmpty() || m.getDateTime().until(currentConvo.getMessageList().get(currentConvo.getMessageList().size() - 1).getDateTime(), ChronoUnit.MINUTES) <= allowedMinutes) {
            currentConvo.addMessage(m);
            continue;
        }
        if (currentConvo.getAllParticipants().size() > 1) {
            // only if more than one participant is involved it is a conversation
            result.add(currentConvo);
        }
        // not more than one person involved and also last message is too long ago -> discard the current conversation
        currentConvo = new Chat();
        currentConvo.addMessage(m);
    }
    return result;
}

谢谢你的回复。

但你是;例外情况不是对你撒谎。有可能是另一个线程正在执行此操作;更可能是forloop中的某一行导致了更改。例如,
addMessage
命令可能会找出该消息已经是哪个聊天室的一部分,并将其从该聊天室中删除–即从您处,更改列表


编辑:正如Andreas所评论的,另一个合理的解释是您意外地创建了
messageList
字段
static
。在这种情况下,所有不同的聊天对象共享一个列表,因此,将消息添加到新创建的聊天对象仍然是将其添加到单个全局列表中。如果是这种情况,解决方案是:使其非静态。

堆栈跟踪中的行
188
是什么?这就是抛出异常的地方。您没有显示代码的其他部分,目前也没有任何不可变性。如果在循环期间也尝试修改,则会发生错误。考虑使用不可变列表(CopyOnWrordRayList),这是一个很好的方法,或者在循环过程中找到一个不更新列表的方法。理论:您的<代码>聊天> /代码>类将消息存储在<代码>静态< /代码>字段中,因此即使您的代码使用2个不同的<代码>聊天对象,但只有一个列表,这意味着<代码> CurrnCurv.AddiMead。(m) 修改由
this.getMessageList()返回的列表;
,假设
this
是一个
聊天对象(请参见stacktrace)。---这当然只是一个猜测,因为您没有共享相关代码,但这是一个有根据的猜测,因为
currentConva=new Chat();currentConva.addMessage(m)
否则就毫无意义了,添加到一个新的聊天中,然后扔掉这个对象。更有可能的是,类
聊天
消息列表
字段是
静态的
。看。哦,是的-这是一个非常合理的解释。
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chatstats, PID: 22185
java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.next(ArrayList.java:860)
    at com.example.chatstats.Chat.getAllConversations(Chat.java:188)
    at com.example.chatstats.MainActivity.onClickShowResults(MainActivity.java:169)
    at com.example.chatstats.MainActivity$9.onClick(MainActivity.java:152)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
I/zygote: WaitForGcToComplete blocked for 14.815ms for cause HeapTrim
Disconnected from the target VM, address: 'localhost:8622', transport: 
'socket'