在做项目中用到List存储数据,在里面做数据操作时候用到了删除.结果抛出ConcurrentModificationException异常.在这里把问题总结一下. 原因: ArrayList进行foreach时所调用的迭代器(内部迭代器Itr) /** * An optimized version of AbstractList.Itr */ private class Itr implements Iterator<E> { int cursor; // index of next elem…
一.ConcurrentModificationException ArrayList源码看为什么出现异常: public class ArrayList<e> extends AbstractList<e> implements Cloneable, Serializable, RandomAccess { @Override public boolean remove(Object object) { Object[] a = array; int s = size; if (…
java中两种基本的集合结构ArrayList和LinkedList底层有两种不同的存储方式实现,ArrayList为数组实现,属于顺序存储,LinkedList为链表实现,属于链式存储,在对ArrayList做迭代删除时,会出现ConcurrentModificationException public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add(&…
最朴实的方法,使用下标的方式: ArrayList<String> al = new ArrayList<String>(); al.add("a"); al.add("b"); //al.add("b"); //al.add("c"); //al.add("d"); for (int i = 0; i < al.size(); i++) { if (al.get(i) ==…
ArrayList关键属性分析 ArrayList采用Object数组来存储数据 /** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. 何问起 hovertree.com */ transient Object[] elementData; // non-priva…