0

I have some problem with java.util.ConcurrentModificationException. When I deleted one characater or more in my autocompletetext I got force close. Anybody knows what happen with that ? and what is the solution for that problem ?

Thank You.

Nicholas
  • 119
  • 2
  • 17

1 Answers1

0

It looks like you are adding and deleting something from your collection at same point of time. By doing this, you are structurally modifying the collections more than once at single point of time. Hence you are getting java.util.ConcurrentModificationException, which is the result of "Fail-Fast" iterators being used in your collections. You can have a look at this link which explains fail-safe and fail-fast iterators. what-is-fail-safe-fail-fast-iterators-in-java-how-they-are-implemented and checkout the answer written by Stephen C.

Community
  • 1
  • 1
Shyam Baitmangalkar
  • 1,075
  • 12
  • 18
  • Do you have some example of code to solve that problem ? – Nicholas Jul 15 '16 at 02:20
  • It's pretty simple, like I said, if you are trying to modify your collection structurally at a same given point of time, the iterator which you are using to iterate through that collection will fail. Check this example snippet below: `ArrayList sampleList = new ArrayList(); sampleList.add("hello"); sampleList.add("world"); Iterator sampleListItr = sampleList.iterator(); //If you try to remove first item from list, your iterator fails sampleList.remove(0); while(sampleListItr.hasNext()){ System.out.println(sampleListItr.next()); }` So try to avoid iterators. – Shyam Baitmangalkar Jul 15 '16 at 05:39
  • You can avoid that problem by not using iterators. Java 8 provides you `foreach` method to help you iterate over any collections. – Shyam Baitmangalkar Jul 15 '16 at 05:45