ava中的ArrayList循環遍歷並且刪除元素時經常不小心掉坑里,昨天又碰到了,感覺有必要單獨寫篇文章記一下。
先寫個測試代碼:
- import java.util.ArrayList;
- public class ArrayListRemove {
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<String>();
- list.add("a");
- list.add("bb");
- list.add("bb");
- list.add("ccc");
- list.add("ccc");
- list.add("ccc");
- remove(list);
- for (String s : list) {
- System.out.println("element : " + s);
- }
- }
- public static void remove(ArrayList<String> list) {
- // TODO:
- }
- }
錯誤寫法示例一:
- public static void remove(ArrayList<String> list) {
- for (int i = 0; i < list.size(); i++) {
- String s = list.get(i);
- if (s.equals("bb")) {
- list.remove(s);
- }
- }
- }
這種最普通的循環寫法執行后會發現有一個“bb”的字符串沒有刪掉。
錯誤寫法示例二:
- public static void remove(ArrayList<String> list) {
- for (String s : list) {
- if (s.equals("bb")) {
- list.remove(s);
- }
- }
- }
這種for each寫法會發現報出著名的並發修改異常java.util.ConcurrentModificationException。
要分析產生上述錯誤現象的原因唯有翻一翻jdk的ArrayList源碼,先看下ArrayList中的remove方法(注意ArrayList中的remove有兩個同名方法,只是入參不同,這里看的是入參為Object的remove方法)是怎么實現的:
- 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;
- }
按一般執行路徑會走到else路徑下最終調用faseRemove方法:
- 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; // Let gc do its work
- }
可以看到會執行System.arraycopy方法,導致刪除元素時涉及到數組元素的移動。針對錯誤寫法一,在遍歷第二個元素字符串bb時因為符合刪除條件,所以將該元素從數組中刪除,並且將后一個元素移動(也是字符串bb)至當前位置,導致下一次循環遍歷時后一個字符串bb並沒有遍歷到,所以無法刪除。
針對這種情況可以倒序刪除的方式來避免:
- public static void remove(ArrayList<String> list) {
- for (int i = list.size() - 1; i >= 0; i--) {
- String s = list.get(i);
- if (s.equals("bb")) {
- list.remove(s);
- }
- }
- }
因為數組倒序遍歷時即使發生元素刪除也不影響后序元素遍歷。
而錯誤二產生的原因卻是foreach寫法是對實際的Iterable、hasNext、next方法的簡寫,問題同樣處在上文的fastRemove方法中,可以看到第一行把modCount變量的值加一,但在ArrayList返回的迭代器(該代碼在其父類AbstractList中):
- public Iterator<E> iterator() {
- return new Itr();
- }
這里返回的是AbstractList類內部的迭代器實現private class Itr implements Iterator<E>,看這個類的next方法:
- public E next() {
- checkForComodification();
- try {
- E next = get(cursor);
- lastRet = cursor++;
- return next;
- } catch (IndexOutOfBoundsException e) {
- checkForComodification();
- throw new NoSuchElementException();
- }
- }
第一行checkForComodification方法:
- final void checkForComodification() {
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- }
這里會做迭代器內部修改次數檢查,因為上面的remove(Object)方法把修改了modCount的值,所以才會報出並發修改異常。要避免這種情況的出現則在使用迭代器迭代時(顯示或foreach的隱式)不要使用ArrayList的remove,改為用Iterator的remove即可。
- public static void remove(ArrayList<String> list) {
- Iterator<String> it = list.iterator();
- while (it.hasNext()) {
- String s = it.next();
- if (s.equals("bb")) {
- it.remove();
- }
- }
- }