1

I can be the implementation of a chat app to show users chat only the can get user_id to be sent or receive a message to store in user-list and select the user to be added in the list some error like a User list if added user 1 and add user 2 when and user 1 again the exception in ArrayList enter image description here

 for (String id : usersList)
                    if (equal(user.getId(), id)) {
                        if (mUseres.size() != 0) {
                            for (User user1 : mUseres) {
                                if (!equal(user.getId(), user1.getId())) {
                                    mUseres.add(user);
                                }
                            }
                        } else {
                            mUseres.add(user);
                        }
0

2 Answers 2

2

The issue is here:

   for (User user1 : mUseres) {
       if (!equal(user.getId(), user1.getId())) {
       mUseres.add(user);
    }

You are iterating mUseres (for....) and at same time you are adding new items (mUseres.add)

Sign up to request clarification or add additional context in comments.

Comments

0

You cant modify the List while iterating through it.

From the docs

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. 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 throw this exception

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.