Java Modify collection throws Concurrentmodificationexception exception

Source: Internet
Author: User
Tags concurrentmodificationexception

The Test code is:

public static void Main (string[] args) {list<string> strlist = new arraylist<string> (); Strlist.add ("1"); Strlist.add ("2"), Strlist.add ("3"), Strlist.add ("4"), for (String str:strlist) {if (Str.equals ("4")) {Strlist.remove ( str);}}}

After the run, the results are as follows:

Trace the code, throw the exception in place specific to:

Public E Next () {checkforcomodification ();    try {E next = get (cursor); Lastret = Cursor++;return next;    } catch (Indexoutofboundsexception e) {checkforcomodification (); throw new Nosuchelementexception (); }}

Before returning an element, the Checkforcomodification () function is called to check that the function is implemented as:

final void Checkforcomodification () {if (Modcount! = expectedmodcount) throw new Concurrentmodificationexception ();

Modcount is the number of times the ArrayList is modified, Expectedmodcount is the number of changes expected,Modcount and Expectedmodcount are 4 before the fourth element is deleted. When the Remove () method of ArrayList is called,Modcount becomes 5, but the Expectedmodcount is still 4, so the above exception occurs.

Workaround: Use the iterator Remove () method instead of the ArrayList remove () method to avoid concurrentmodificationexception exceptions.

Coincidentally, this exception is not thrown if the element to be deleted is exactly the second-to-last element in the collection. iterator traversal gets an element before it calls the Hasnext () method to determine if there are any elements, the method is implemented as:

public Boolean Hasnext () {return cursor! = size ();}

Where the cursor is the traversal position, starting with 0, is updated in the next () method above.

After deleting the element "3", thecursor has a value of 3, and size () returns 3,Hasnext () returns False, the next () method is not called, and therefore does not exist Concurrentmodificationexception exception .

When the element "4" is deleted,the cursor value becomes 4, and size () returns 3,Hasnext () returns True, and the Next () method is thrown when it is called Concurrentmodificationexception exception .



Attach a piece of information:

Java concurrentmodificationexception exception Causes and workarounds: http://www.cnblogs.com/dolphin0520/p/3933551.html








Java Modified collection throws Concurrentmodificationexception exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.