前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一道面试题引发的思考

一道面试题引发的思考

作者头像
阿豪聊干货
发布2018-08-09 10:00:40
1680
发布2018-08-09 10:00:40
举报
文章被收录于专栏:Java后端技术Java后端技术

一、背景及题目

首先我们给出这道面试题的代码以及题目:

代码语言:javascript
复制
List<String> a = new ArrayList<String>(); 
list.add("1");
list.add("2");
for (String item : list) {
    if ("1".equals(item)) { list.remove(item);
    } 
}

问:上段代码运行会报错吗?如果把”1”换成“2”会报错吗?为什么?

二、解开这段代码背后的秘密

首先给出答案:

  1. 上面这段代码运行不会报错。
  2. 把”1”换成“2”再运行就会报错。

为什么呢?那么我们怎么来发现它背后的秘密呢?答案只有一个:那就是通过源码来解惑(ArrayList部分源码)。

代码语言:javascript
复制
public E remove(int index) {
    rangeCheck(index);

    modCount++;
    E oldValue = elementData(index);

    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

    return oldValue;
}

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;
}

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
}
/**
 * An optimized version of AbstractList.Itr
 */
private class Itr implements Iterator<E> {
    int cursor;       // index of next element to return
    int lastRet = -1; // index of last element returned; -1 if no such
    int expectedModCount = modCount;

    public boolean hasNext() {
        return cursor != size;
    }

    @SuppressWarnings("unchecked")
    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];
    }

    public void remove() {
        if (lastRet < 0)
            throw new IllegalStateException();
        checkForComodification();

        try {
            ArrayList.this.remove(lastRet);
            cursor = lastRet;
            lastRet = -1;
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    }

    final void checkForComodification() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
    }
}
  • 我们知道,foreach循环其实是走的list的迭代器进行循环的。
  • 迭代器中有一个"指针":cursor来记录当前遍历到了哪个位置,还有一个lastRet变量来表示上次返回的值的位置。
  • foreach每次循环时先调用迭代器的hastNext()方法,判断cursor != size。
  • 然后调用迭代器的next()方法,在方法中会首先调用checkForComodification()判断是否要抛出ConcurrentModificationException(判断条件是modCount != expectedModCount,默认两个值相等),然后修改cursor和lastRet变量的值,并返回下一个元素的值。
  • ArrayList的remove()方法会去把modCount增加1,若再次进行第3步则抛异常。
  • 但删除倒数第二个元素时remove()后size-1,所以会导致在hasNext()方法返回false,最后一个值不遍历,不遍历也就意味着不会调用checkForComodification()。
  • 迭代器中的remove()方法会把cursor修改为lastRet,然后把modCount修改为expectedModCount,所以无论怎么删除都不会出错。
  • 遍历下标remove(index)方法不会走迭代器,所以无论怎么删除也不会出错。

三、总结

  我们通过查询ArrayList的源码,可以清楚的知道,它的内部是有一个迭代器类的,然后它的底层其实就是一个数组而已。对于要删除一个ArrayList中的某些元素的时候,我们可以通过遍历下标,找到要删除的元素,直接通过下标删除,或者通过ArrayList的迭代器进行删除,千万不能直接用foreach遍历删除。还有就是遇见问题看到表象要想着去找本质,懂了原理才能知其然知其所以然。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、背景及题目
  • 二、解开这段代码背后的秘密
  • 三、总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档