When looking at ArrayList source code, saw a field modcount. In the Add, remove, clear and other methods have modcount++ operation. Don't understand what it means. The point went in and looked at the explanation of the field, and finally understood. Modcount is defined in the Abstractlist abstract class. The explanation for this field is as follows.
/** * the number ofTimes ThisList has been <i>structurally modified</i>. * Structural Modifications is those that the size ofThe * list,orotherwise perturb itinchSuch a fashion that iterationsinch* Progress may yield incorrect results. * The number of structural changes to the list. A structural change is a change in the size of a list, or the order being scrambled, causing the iterations to get the wrong result during execution. * * <p>this Field isUsed byThe iterator andList Iterator Implementation * returned byThe {@codeIterator and{@codeListiterator} methods. * If the value of ThisField changes unexpectedly, the iterator (orList * iterator) wouldThrowA@codeConcurrentmodificationexception}inch* Response to the {@codeNext}, {@codeRemove}, {@codePrevious}, * {@codeSetor{@codeAdd} operations. This provides * <i>fail-fast</i> behavior, rather than non-deterministic behaviorinch* The face ofConcurrent modification during iteration. * This field is used for the iterator and list iterator returned by the iterator and Listiterator methods to implement data iterations. * If there is an unexpected change in the field, Iterator/list iterator will throw a concurrentmodificationexception when performing actions such as Next, remove, Previous, * set, add, and so on. This provides a quick failure behavior that does not occur during the execution of an iterative process of indeterminate behavior. * * <p><b>use of ThisField bySubclasses isOptional.</b> If A subclass * wishes to provide Fail-fast iterators ( andList iterators), ThenIt * merely have to increment ThisFieldinchits {@codeAdd (int, E)} and* {@codeRemove (int)} methods ( andAny other methods the IT overrides * that resultinchStructural modifications to the list). A single call to * {@codeAdd (int, E)}or{@codeRemove (int)} must addNoMore than * one to ThisFieldorThe iterators ( andList iterators) wouldThrow* Bogus {@codeConcurrentmodificationexceptions}. If an implementation * does notwish to provide Fail-fast iterators, ThisField May is * ignored. * It is optional for subclasses to use this field. If a subclass wants to provide a fast-failing iterator/list iterator, the * only needs to increase the value of the field in its add, remove, or other overloaded methods that cause the list structure to change. * A call to add or remove does not increase the value of this field more than1, otherwise iterator/list iterator * will throw false concurrentmodificationexceptions. If the implementation class does not want to provide a fast-failing iterator, the field can be ignored. */protected transient int modcount =0;//If you do not know what transient do, please consciously go to wall?
For Concurrentmodificationexception, please refer to: Java elapsed time Delete elements from list, Set, map (source code Analysis)
The Modcount and Concurrentmodificationexception in ArrayList