View source code we can see that there is a modcount field in the list that will report an concurrentmodificationexception exception if the field is modified during the iteration.
Here's a look at the relevant source code
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/00/wKiom1XxbGSQG4e_AAViuYUvjKw407.jpg "title=" 2015-09-10_193952.png "alt=" Wkiom1xxbgsqg4e_aaviuyuvjkw407.jpg "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/72/FD/wKioL1XxbqSC9bcRAAEs0pAU6Wk356.jpg "title=" 2015-09-10_194041.png "alt=" Wkiol1xxbqsc9bcraaes0pau6wk356.jpg "/>
In the initial state, the value of Modcount is 0, the value of Expectedmodcount at the beginning of the iteration is also 0, if an element is deleted during the iteration, the value of Modcount becomes 1 after modcount++, then the next iteration (called Next method) The Checkforcomodification method will be adjusted to check whether the Modcount and expectedmodcount are equal, obviously at this time the two are not equal, so throw an exception.
Here is the test code
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/01/wKiom1XxbW7yZyF6AAONRmuOP9w194.jpg "title=" 2015-09-10_193416.png "alt=" Wkiom1xxbw7yzyf6aaonrmuop9w194.jpg "/>
Careful readers will find that the above code, when equal to a or C when the deletion will throw an exception, and equal to B when the deletion will not throw an exception, in fact, this is a coincidence. Because, after the first iteration of the deletion of a, cursor=1,size=2, and then into the next iteration, so in next detects that Modcount and expectedmodcount are not equal, throw an exception. When the hypothesis equals B is deleted, then after the second iteration delete B, cursor=2,size=2,cursor and size are equal, so the Hasnext () method returns false, so no iterations, so there is no chance to error.
This article is from the "ten-li Rice flower" blog, please be sure to keep this source http://5880861.blog.51cto.com/5870861/1693563
List Iteration Times concurrentmodificationexception exceptions