The understanding and modification of Java.util.ConcurrentModificationException

Source: Internet
Author: User

In the Java development process encountered similar exception information Exception in thread "main" java.util.ConcurrentModificationException,

The following is a description of the cause of the exception and the improvement of the exception, the content is very simple, there are any problems to be corrected.

Suppose we want to implement an example of whether there is a "world" element in the set, and if so, add a "Java ee" element

The code for the exception appears as follows:

Import java.util.ArrayList;Import Java.util.Iterator;PublicClass Test {PublicStaticvoid Main (string[] args) {arraylist<string> array = new arraylist<string> (); //Create and add Element array.add ("Hello"); Array.add ("World"); Array.add ("Java"); Iterator it = array.iterator (); while (It.hasnext ()) { String s = (string) it.next (); if ("World". Equals (s)) { Array.add ("Java ee");        }}} 

1. Exception explanation

    • Concurrentmodificationexception: This exception is thrown when the method detects concurrent modifications to an object, but does not allow this modification.
    • Causes of the production:
         迭代器是依赖于集合而存在的,在判断成功后,集合的中新添加了元素,而迭代器却不知道,所以就报错了,这个错叫并发修改异常。   简单描述就是:迭代器遍历元素的时候,通过集合是不能修改元素的。
    • How to solve it?
         A:迭代器迭代元素,迭代器修改元素   B:集合遍历元素,集合修改元素(普通for)

2. Here are two ways to resolve this exception

Import java.util.ArrayList;PublicClass Test {PublicStaticvoid Main (String[] args) {arraylist<String>Array =New arraylist<String> ();Creating and adding elementsArray.add ("Hello");Array.add ("World");Array.add ("Java");Method 1: Iterator iteration element, iterator modification elementThe iterator iterator does not add functionality, so we use its sub-interface ListiteratorListiterator lit = Array.listiterator ();while (Lit.hasnext ()) {string s = (string) lit.next ();if ("World". Equals (s)) {Lit.add ("Java ee");// }// }System.out.println ("List1:" + array);Method 2: Set traversal element, set modify element (normal for)for (int x =0; X <Array.Size (); X + +) {string s = (string) array.get (x); if ( "World". Equals (s)) { "java ee");}} System.out. println ( "List2:" + array); //mode 3: Enhanced for loop //enhanced for Loop writes the same error because it is itself a substitute for the iterator's //for (String s:array) {//if ("World". Equals (s)) {//array.add (" Java ee "); //} //} //System.out.println ("LIST3:" + Array);}}        

Java.util.ConcurrentModificationException's understanding and modification

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.