当前位置:网站首页>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 :
边栏推荐
- Opencv image storage and reading
- Advanced C language - Section 1 - data storage
- Force buckle 92 Reverse linked list II
- Chapter 14 introduction: memory operation API
- Elliptic curve encryption
- C language learning log 10.10
- Dup2 use
- Interpretation of QT keypressevent
- C language learning log 10.19
- Mind mapping series - Database
猜你喜欢
Win8.1和Win10各自的優勢
Simple greedy strategy
使用EasyDarwin+FFmpeg实现rtsp推流
C # get all callable methods of WebService interface [webmethod]
QT direction key to move focus
[JS solution] leedcode 200 Number of islands
Recursion and recursion
Kaggle time series tutorial
Simple SR: best buddy Gans for highly detailed image super resolution
Robot pose description and coordinate transformation
随机推荐
Section 8 - Practical commissioning techniques
C language learning log 11.7
The games that you've tasted
C language learning log 10.10
shell变量学习笔记
Analysis of the principle of V-model and its application in user defined components
C language learning log 1.2
Avantages de win8.1 et win10
Clause 48: understand template metaprogramming
[leetcode]- sliding window
【多线程编程】Future接口获取线程执行结果数据
C language learning log 12.14
RuoYi-Cloud启动教程(手把手图文)
Sub paragraph of Chapter 16
Chapter 2 process management
【转载】C语言内存和字符操作函数大全
Opencv image storage and reading
QT direction key to move focus
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
C language learning log 1.17