November 15, 2020
When I wrote the code in Java and checked if it was using the test, I got an exception called
java.util.ConcurrentModificationException, so I will summarize what the ConcurrentModificationException is and what to do.
Javadoc for ConcurrentModificationException states as follows.
This exception is thrown when a method that detects a parallel change in an object does not allow such a change.
Basically, it is used to fail-first to fail early and throw an exception when something that repeats changes </ b>. That is, an exception has occurred before the iterative process is completed.
The for-each statement uses Iterator, which isn't very verbose, but you can access additional methods such as
remove () if you refactor your test to use Iterator. The remove method does not cause a ConcurrentModificationException, so it is okay to call it during an iterative process.
In my code, I had a problem with how to handle ArrayList. ArrayList and Iterator internally hold the "number of changes". The Iterator generated by the Iterator () method of ArrayList is checking whether there is a deviation in the number of changes when starting the next () method etc. used when turning with for, and because there is a deviation, an exception of ConcurrentModificationException Was occurring. There was the following description in Javadoc ArrayList.
The iterator returned by the iterator and listIterator methods of this class is failfast. After creating an iterator, the iterator throws a ConcurrentModificationException if the list is structurally modified by any method other than the remove or add method of the iterator itself. In this way, when changes are made in parallel, the iterator throws an exception immediately and gracefully to avoid the risk of unpredictable behavior at unpredictable points in the future. Iterator failfast behavior cannot be guaranteed, as there is usually no reliable guarantee when there are asynchronous parallel changes. The failfast iterator throws a ConcurrentModificationException based on the best effort principle. Therefore, it is a mistake to write a program that depends on this exception for accuracy. "Iterator failfast behavior should only be used to detect bugs."