当前位置:网站首页>List collection concurrent modification exception

List collection concurrent modification exception

2022-06-13 05:02:00 Jason_ LH1024

java Source code analysis :

public interface List<E>{
Iterator<E> iterator();
boolean add(E e);
}
public abstract class AbstractList<E>{
int modCount = 0;
}

//ArrayList Inherited a class and implemented an interface 
public class ArrayList<E> extends AbstractList<E> implements List<E>{

    public boolean add(E e) {
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            elementData[size++] = e;
            return true;
        }


         public Iterator<E> iterator() {
            return new Itr();
           }

       private class Itr implements Iterator<E> {

               int expectedModCount = modCount;
               /*
                modCount: The number of actual changes to the set 
                expectedModCount: The number of expected changes to the collection 


               */

               public E next() {
                   checkForComodification();
                   int i = cursor;
                   if (i >= size)
                       throw new NoSuchElementException();
                   Object[] elementData = ArrayList.this.elementData;
                   if (i >= elementData.length)
                       throw new ConcurrentModificationException();
                   cursor = i + 1;
                   return (E) elementData[lastRet = i];
               }



               final void checkForComodification() {
                   if (modCount != expectedModCount)
                       throw new ConcurrentModificationException();
               }
           }
  }
}

Test and solve method implementation :

package com.it03;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        // Create a collection object 
        List<String> list = new ArrayList<String>();

        // Additive elements 
        list.add("hello");
        list.add("ssss");
        list.add("world");

        // Ergodic set , See if there's any world This element , If there is one, I will add one javaee This element , Code implementation 
//        Iterator<String> it = list.iterator();
//        while (it.hasNext()){
//            String s = it.next();
//            if(s.equals("world")){
//                list.add("javaee");
//            }
//        }

        for (int i = 0; i <list.size() ; i++) {
            String s = list.get(i);
            if(s.equals("world")){
                list.add("javaee");
            }

        }

        // Output collection object 
        System.out.println(list);


    }
}

  Error reporting under iterator exception :

Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at com.it03.ListDemo.main(ListDemo.java:21)

Output after modification :

原网站

版权声明
本文为[Jason_ LH1024]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280515588409.html