ArrayList源码解读三之Iterator(迭代器)

ArrayList源码解读三之Iterator(迭代器)

在ArrayList的iterator()方法中就只有创建一个类Itr。
public Iterator<E> iterator() {
    return new Itr();
}
我们查看一下Itr这个类,发现它是实现了 Iterator接口的。
private class Itr implements Iterator<E> {
    int cursor;       
    int lastRet = -1; 
    int expectedModCount = modCount;
    Itr() {}
}
我们了解方法之前先了解变量作用
  • cursor:表示光标,其作用是确定此时光标在位置的
  • lastRet:默认是无元素状态,其作用是确定位置有元素的。
    ( lastRet=-1表示此位置无元素,lastRet表示此位置有元素)
  • modCount:记录ArrayList的操作次数
  • expectedModCount:记录Iterator的操作次数
清楚了解变量的作用,我们接下来了解方法

(一)checkForComodification函数
作用是判断ArrayList的操作次数与Iterator的操作次数是否相一致,不一致则抛出异常

final void checkForComodification() {
    if (modCount != expectedModCount)
        throw new ConcurrentModificationException();
}

(二)next函数

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

步骤:

  1. 进行判断操作次数是否一致。
  2. 判断光标是否大于ArrayList数组的元素数量。
  3. 将ArrayList进行赋值给新的Object数组。
  4. 判断光标是否大于ArrayList数组的大小。
  5. 对光标进行+1。
  6. 将lastRet赋值,并且返回。

(三)remove函数

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

步骤:

  1. 判断lastRet的位置是否有元素
  2. 进行判断操作次数是否一致。
  3. 对ArrayList的数组进行移除当前lastRet位置的元素。
  4. 对光标进行移动。
  5. 将lastRet此时位置设置无元素。
  6. 更新expectedModCount修改次数。

注意:在使用过next函数前提下才能使用remove()函数,不然会抛出IllegalStateException()异常。

(四)forEachRemaining函数
作用:与foreach类似,forEachRemaining遍历Iterator的所有元素

public void forEachRemaining(Consumer<? super E> consumer) {
        Objects.requireNonNull(consumer);
        final int size = ArrayList.this.size;
        int i = cursor;
        if (i >= size) {
            return;
        }
        final Object[] elementData = ArrayList.this.elementData;
        if (i >= elementData.length) {
            throw new ConcurrentModificationException();
        }
        while (i != size && modCount == expectedModCount) {
            consumer.accept((E) elementData[i++]);
        }
        // update once at end of iteration to reduce heap write traffic
        cursor = i;
        lastRet = i - 1;
        checkForComodification();
    }

步骤:

  1. requireNonNull判断consumer对象是否为空指针。
  2. 获取ArrayList中的元素数量、获取光标位置、创建新的Oject数组。
  3. 光标超过ArrayList的数量或者数组长度会返回或者抛出异常。
  4. 光标不等于ArrayList的数量以及Iterator和ArrayList操作次数一致的情况下进行循环。
    (consumer.accept里面是空函数,具体函数内容模块需要传进来的consumer实现)
  5. 对光标,以及lastRet赋值
  6. 判断Iterator和ArrayList操作次数是否一致

以上就是ArrayList中Iterator的源码实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值