JavaSE基础知识(二十一)--Java集合(容器)之类LinkedList的内部类ListItr的源码分析

方法previous()图解
方法next()图解

    //列表迭代器
    private class ListItr implements ListIterator<E> {
       //迭代器最近一次返回的节点
       private Node<E> lastReturned;
       //迭代器即将要返回的节点
       private Node<E> next;
       //迭代器即将要返回的节点的索引
       private int nextIndex;
       //通过内部类变量expectedModCount
       //保存外部类变量modCount的值
       //以此保证外部类与内部类的快速失
       //败机制的同步,在内部类方法执行的时候
       //modCount如果改变,那么必然与expectedModCount
       //的值不等,直接触发快速失败机制
       private int expectedModCount = modCount;
       //构造方法,创建一个从指定索引
       //index开始的迭代器
       ListItr(int index) {
          //如果index==size,则next为null
          //因为索引size处没有节点
          //否则,next是索引值为index的节点
          //参考LinkedList的方法node(int index)
          next = (index == size) ? null : node(index);
          //初始化变量nextIndex,索引为index
          //的节点就是即将要返回的节点
          nextIndex = index;
       }
       //很多人会认为这里是判断
       //是否有下一个节点,实际上不是
       //这个方法就是判断nextIndex
       //是否小于size
       public boolean hasNext() {
          //节点的最大索引值为size-1
          return nextIndex < size;
       }
       
       //返回节点的元素
       //迭代的方向是从左至右
       public E next() {
          //检查列表是否发生结构性修改
          checkForComodification();
          //如果nextIndex不小于size
          if (!hasNext())
             //直接抛出NoSuchElementException异常
             throw new NoSuchElementException();
          //next已经在构造方法中进行了初始化
          //如果next()方法顺利执行完毕,next
          //引用的节点元素会被返回,同时断开next
          //与节点的引用关系,此节点成为迭代器
          //最近一次返回的节点,所以在断开前
          //需要将节点的引用传递给lastReturned
          //否则节点会被垃圾回收器回收
          lastReturned = next;
          //断开next引用,指向下一个节点
          //此时lastReturned引用迭代器
          //最近一次返回的节点
          next = next.next;
          //nextIndex的值同步+1
          nextIndex++;
          //方法执行到这里,lastReturned
          //确实是迭代器最近一次返回的节点
          //直接返回节点的元素
          return lastReturned.item;
       }
       //此方法与next()方法对应,判断
       //nextIndex是否大于0
       
       public boolean hasPrevious() {
          return nextIndex > 0;
       }
       //返回节点的元素
       //迭代的方向是从右至左
       public E previous() {
          //检查列表是否发生结构性修改
          checkForComodification();
          if (!hasPrevious())
             //直接抛出NoSuchElementException异常
             throw new NoSuchElementException();
          lastReturned = next = (next == null) ? last : next.prev;
          //从右至左步进后,nextIndex的值同步-1
          nextIndex--;
          //返回节点的元素
          //将next的值复制给lastReturned后
          //lastReturned指向返回的节点
          return lastReturned.item;
       }
       //返回下一个节点的索引值
       public int nextIndex() {
          return nextIndex;
       }
       //返回上一个节点的索引值
       public int previousIndex() {
          return nextIndex - 1;
       }
       //删除节点
       public void remove() {
          checkForComodification();
          if (lastReturned == null)
              throw new IllegalStateException();

          Node<E> lastNext = lastReturned.next;
          unlink(lastReturned);
          if (next == lastReturned)
              next = lastNext;
          else
              nextIndex--;
          lastReturned = null;
          expectedModCount++;
       }
       //用包含元素e的节点代替节点
       public void set(E e) {
          if (lastReturned == null)
              throw new IllegalStateException();
          checkForComodification();
          lastReturned.item = e;
       }
       //添加节点
       public void add(E e) {
          checkForComodification();
          lastReturned = null;
          if (next == null)
              linkLast(e);
          else
              linkBefore(e, next);
          nextIndex++;
          expectedModCount++;
       }
       //根据条件过滤链表
       public void forEachRemaining(Consumer<? super E> action) {
          Objects.requireNonNull(action);
          while (modCount == expectedModCount && nextIndex < size) {
              action.accept(next.item);
              lastReturned = next;
              next = next.next;
              nextIndex++;
          }
          checkForComodification();
       }
       //快速失败机制
       final void checkForComodification() {
          if (modCount != expectedModCount)
              throw new ConcurrentModificationException();
       }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值