Java Common error Analysis

Source: Internet
Author: User

1: java.util.ConcurrentModificationException error occurred while traversing list remove element

@Test Public voidtest3 () {List<String> list =NewArraylist<string>(); List.add ("1"); List.add ("2");  for(String temp:list) {//Why did it go wrong?             if("2". Equals (temp))               {List.remove (temp); }        }    }//or below@Test Public voidtest4 () {List<String> list =NewArraylist<string>(); List.add ("1"); List.add ("2"); List.add ("3"); List.add ("4"); Iterator Iterator=List.iterator ();  while(Iterator.hasnext ()) {String temp= (String) iterator.next ();//Error            if("2". Equals (temp))             {List.remove (temp); }        }    }

Explanation of this exception in the API: This exception is thrown when the method detects concurrent modifications to an object, but does not allow this modification.

Reason:

Iterator is working in a separate thread, and has a mutex lock. Iterator is created after the creation of a single-chain index table pointing to the original object, when the original object number changes, the contents of the index table will not be synchronized changes, so when the index pointer moves back to find the object to iterate, so according to Fail-fast principle Iterator The java.util.ConcurrentModificationException exception is thrown immediately.

The For loop list is actually equivalent to:

Iterator ite = List.iterator ();
if (Ite.hasnext ()) {
NV = Ite.next ();
At this point you call (omit part of the code)
List.remove (NV);
}

So Iterator is not allowed to be changed by iterative objects while working. But you can delete the object using Iterator's own method, remove (), and the Iterator.remove () method maintains the consistency of the index while deleting the current iteration object.

@Testpublic void Test5 () {list<string> List = new arraylist<string> () List.add ("1"); List.add ("2"); List.add ("3"); List.add ("4"); Iterator Iterator = List.iterator (); while (Iterator.hasnext ()) {String temp = (string) Iterator.next (); if ("2". Equals (temp)) {iterator.remove ();}} SYSTEM.OUT.PRINTLN (list);}

  

Java Common error analysis

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.