关于ListInterator迭代器游标的逻辑与底层原理

1. ListInterator迭代器游标的逻辑与底层原理

首先我们先引入两个变量:

  1. cursor:表示下一个元素的索引位置
  2. lastRet:表示上一个元素的索引位置

1.1 获取迭代器后:curse = 0;lastRet = -1

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

next()执行过程:

  1. curse = curse+1;
  2. lastRet = 原来的curse;
  3. 返回lastRet处元素

1.3 previous()方法

public E previous() {
    checkForComodification();
    int i = cursor - 1;
    if (i < 0)
        throw new NoSuchElementException();
    Object[] elementData = ArrayList.this.elementData;
    if (i >= elementData.length)
        throw new ConcurrentModificationException();
    cursor = i;
    return (E) elementData[lastRet = i];
}

previous()执行过程:

  1. cursor=cursor-1;
  2. lastRet=cursor;
  3. 返回lastRet处元素

1.4 add(E e)

public void add(E e) {
    checkForComodification();

    try {
        int i = cursor;
        ArrayList.this.add(i, e);
        cursor = i + 1;
        lastRet = -1;
        expectedModCount = modCount;
    } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
    }
}

add(E e)执行过程:

  1. 在curse处创建元素,然后后续元素后移(此时两个变量的指向第一个元素可能改变);
  2. curse = curse+1;
  3. lastRet = -1;

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

remove()执行过程:

  1. 移除lastRet处元素(此时两个变量的指向第几个元素可能改变);
  2. cursor = lastRet;
  3. lastRet = -1;

1.6 set(E e):只看lastRet

public void set(E e) {
    if (lastRet < 0)
        throw new IllegalStateException();
    checkForComodification();

    try {
        ArrayList.this.set(lastRet, e);
    } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
    }
}

remove()执行过程:只看lastRet

2. 结论总结

获取迭代器后curse = 0;lastRet = -1
next()curse = curse+1lastRet = 原来的curse返回lastRet处元素
previous()cursor=cursor-1lastRet=cursor返回lastRet处元素
add(E e)在curse处创建元素,其余元素后移(此时两个变量的指向第一个元素可能改变)curse = curse+1lastRet = -1
remove()移除lastRet处元素(此时两个变量的指向第几个元素可能改变)cursor = lastRetlastRet = -1
set(E e)只看lastRet

3. 例子

3.1 例子1

在这里插入图片描述

注意:图中左边箭头表示curse;右边箭头表示lastRet

3.2 例子2

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ElegantCodingWH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值