How to avoid ConcurrentModificationException when using ArrayList on Android?
Can someone give the basic rule of thumb to avoid it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By not adding/removing the elements when iterating over your arraylist.
This code would throw the ConcurrentModificationException as you are modifying the list while iterating.
This code wouldn’t throw the ConcurrentModificationException.
You could also use some of the class’s from java.util.Concurrent package such as java.util.concurrent.CopyOnWriteArrayList which wouldn’t throw
ConcurrentModificationExceptionwhen you modify the list while iterating: