0

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);
                        }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841

2 Answers2

1

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)

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
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

Arka Prava Basu
  • 2,366
  • 3
  • 18
  • 34