上一篇,我们讲解了:别让for循环毁了你的程序(一),今天我继续讲解for循环还会带来哪些坑。这些坑都是开发中的小细节,都是值得我们注意的。
- . 在遍历ArrayList时调用 ArrayList.remove()方法。
- 举个栗子一:移除所有能被2整除的数
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
System.out.println("原list的长度是:"+list.size());
for(int i=0;i<list.size();i++){
if(list.get(i)%2==0){
list.remove(list.get(i));
}
}
System.out.println("移除后list的长度是:"+list.size());
System.out.println("移除后还剩以下元素:");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+",");
}
好了,执行以下,看一下执行结果:
原list的长度是:8
移除后list的长度是:5
移除后还剩以下元素:
1,2,3,5,7,
发现问题:
好了,看了执行结果,你会发现原长度是8,没问题,移除后的长度是5有问题,移除后剩余元素分别为1,2,3,5,7有问题。因为,会数学的人都知道,2除以2余数是0的,为什么,移除后还有2了。
分析原因:
第三个元素没有remove 掉,跟踪:
第一次循环
i=0 size=5 当前元素=1 不移除元素
i=1 size=5 当前元素=2 移除元素
i=2 size=4 当前元素=3 不移除元素
在remove 的过程中 size 是移动的,但是,你的循环size却是++的,所以 第三个元素给漏掉了。
- 举个栗子二:使用加强for循环继续移除所有能被2整除的数
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
System.out.println("原list的长度是:"+list.size());
for (Integer a:list){
if(a == 2){
list.remove(a);
}
System.out.println("移除后list的长度是:"+list.size());
System.out.println("移除后还剩以下元素:");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+",");
}
}
执行一下,看看最终结果:
原list的长度是:8
移除后list的长度是:8
移除后还剩以下元素:
1,2,2,3,4,5,6,7,移除后list的长度是:7
移除后还剩以下元素:
1,2,3,4,5,6,7
不仅如此,此时,还对外跑出来异常,异常如下:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at com.bonade.multiplefacerecognition.module.main.utils.StringToFloatArray.main(StringToFloatArray.java:100)
此时,程序抛出了ConcurrentModificationException异常,从异常信息可以发现,异常出现在checkForComodification()方法中。
我们不忙看checkForComodification()方法的具体实现,我们先根据程序的代码一步一步看ArrayList源码的实现:
首先看ArrayList的iterator()方法的具体实现,查看源码发现在ArrayList的源码中并没有iterator()这个方法,那么很显然这个方法应该是其父类或者实现的接口中的方法,我们在其父类AbstractList中找到了iterator()方法的具体实现,下面是其实现代码:
public Iterator<E> iterator() {
return new Itr();
}
从这段代码可以看出返回的是一个指向Itr类型对象的引用,我们接着看Itr的具体实现,在AbstractList类中找到了Itr类的具体实现,它是AbstractList的一个成员内部类,下面这段代码是Itr类的所有实现:
private class Itr implements Iterator<E> {
int cursor = 0;
int lastRet = -1;
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
别急,我们先细细分析一下Itr 这个类。
- 首先我们看一下它的几个成员变量:
1. cursor:表示下一个要访问的元素的索引,从next()方法的具体实现就可看出
2. lastRet:表示上一个访问的元素的索引
3. expectedModCount:表示对ArrayList修改次数的期望值,它的初始值为modCount。
4.modCount是AbstractList类中的一个成员变量
protected transient int modCount = 0;
该值表示对List的修改次数,查看ArrayList的add()和remove()方法就可以发现,每次调用add()方法或者remove()方法就会对modCount进行加1操作。
好了,到这里我们再看看上面的程序:
当调用list.iterator()返回一个Iterator之后,通过Iterator的hashNext()方法判断是否还有元素未被访问,我们看一下hasNext()方法,hashNext()方法的实现很简单:
public boolean hasNext() {
return cursor != size();
}
如果下一个访问的元素下标不等于ArrayList的大小,就表示有元素需要访问,这个很容易理解,如果下一个访问元素的下标等于ArrayList的大小,则肯定到达末尾了。
然后通过Iterator的next()方法获取到下标为0的元素,我们看一下next()方法的具体实现:
@SuppressWarnings("unchecked")
public E next() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
int i = cursor;
if (i >= limit)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
这里是非常关键的地方:首先在next()方法中会调用checkForComodification()方法,然后根据cursor的值获取到元素,接着将cursor的值赋给lastRet,并对cursor的值进行加1操作。初始时,cursor为0,lastRet为-1,那么调用一次之后,cursor的值为1,lastRet的值为0。注意此时,modCount为0,expectedModCount也为0。
接着往下看,程序中判断当前元素的值是否为2,若为2,则调用list.remove()方法来删除该元素。
我们看一下在ArrayList中的remove()方法做了什么:
/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If the list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
* (if such an element exists). Returns <tt>true</tt> if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return <tt>true</tt> if this list contained the specified element
*/
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
注意这里重载了多个remove方法,使用时一定要自己用的是哪一个remove方法。
同时,在重载的romove方法里,调用了fastRemove()方法,看看fastRemove()具体都干了什么。
/*
* Private remove method that skips bounds checking and does not
* return the value removed.
*/
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
通过remove方法删除元素最终是调用的fastRemove()方法,在fastRemove()方法中,首先对modCount进行加1操作(因为对集合修改了一次),然后接下来就是删除元素的操作,最后将size进行减1操作,并将引用置为null以方便垃圾收集器进行回收工作。
那么注意此时各个变量的值:对于iterator,其expectedModCount为0,cursor的值为1,lastRet的值为0。
对于list,其modCount为1,size为0。
接着看程序代码,执行完删除操作后,继续while循环,调用hasNext方法()判断,由于此时cursor为1,而size为0,那么返回true,所以继续执行while循环,然后继续调用iterator的next()方法:
注意,此时要注意next()方法中的第一句:checkForComodification()。
在checkForComodification方法中进行的操作是:
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
如果modCount不等于expectedModCount,则抛出ConcurrentModificationException异常。
很显然,此时modCount为1,而expectedModCount为0,因此程序就抛出了ConcurrentModificationException异常。
到这里,想必大家应该明白为何上述代码会抛出ConcurrentModificationException异常了。
关键点就在于:调用list.remove()方法导致modCount和expectedModCount的值不一致。
注意,像使用for-each进行迭代实际上也会出现这种问题。
好了,既然知道原因了,那么如何解决呢?
方式一:在单线程环境下的解决办法
其实很简单,细心的朋友可能发现在Itr类中也给出了一个remove()方法:
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
在这个方法中,删除元素实际上调用的就是list.remove()方法,但是它多了一个操作:
expectedModCount = modCount;
因此,在迭代器中如果要删除元素的话,需要调用Itr类的remove方法。
将上述代码改为下面这样就不会报错了:
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
System.out.println("原list的长度是:"+list.size());
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
Integer integer = iterator.next();
if(integer==2)
iterator.remove(); //注意这个地方
}
System.out.println("移除后list的长度是:"+list.size());
System.out.println("移除后还剩以下元素:");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+",");
}
}
上面的解决办法在单线程环境下适用,但是在多线程下适用吗?看下面一个例子:
方式二:在多线程环境下的解决方法
public class Test {
static ArrayList<Integer> list = new ArrayList<Integer>();
public static void main(String[] args) {
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
Thread thread1 = new Thread(){
public void run() {
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
Integer integer = iterator.next();
System.out.println(integer);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
};
Thread thread2 = new Thread(){
public void run() {
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
Integer integer = iterator.next();
if(integer==2)
iterator.remove();
}
};
};
thread1.start();
thread2.start();
}
}
运行一下结果,会报一下异常:
1
Exception in thread "Thread-0" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at com.bonade.multiplefacerecognition.module.main.utils.StringToFloatArray$Test$1.run(StringToFloatArray.java:127)
有可能有朋友说ArrayList是非线程安全的容器,换成Vector就没问题了,实际上换成Vector还是会出现这种错误。
原因在于,虽然Vector的方法采用了synchronized进行了同步,但是实际上通过Iterator访问的情况下,每个线程里面返回的是不同的iterator,也即是说expectedModCount是每个线程私有。假若此时有2个线程,线程1在进行遍历,线程2在进行修改,那么很有可能导致线程2修改后导致Vector中的modCount自增了,线程2的expectedModCount也自增了,但是线程1的expectedModCount没有自增,此时线程1遍历时就会出现expectedModCount不等于modCount的情况了。
因此一般有2种解决办法:
- 在使用iterator迭代的时候使用synchronized或者Lock进行同步;
- 使用并发容器CopyOnWriteArrayList代替ArrayList和Vector。
有关更多并发问题,可以参考一下资料:
好了,如果觉得对你有所帮助,不防给我一个赞吧。下一遍,我会继续讲解for循环的注意事项,以及for循环优化问题。