迭代器

之前对迭代器的理解一直存在误区
ListIterator
首先迭代器都是实现了Iterator()接口的,这里面定义了一些方法规范,我们的集合框架都要实现Iterable接口,他里面定义了一个返回迭代器的方法,也就是说,所有的集合框架都要有迭代器让外界拿到用来对集合进行遍历

迭代器没有当前元素一说,他只是一个游标(cursor),这个游标总在元素之间
在这里插入图片描述
每次调用next()方法游标向后移动一位,跨过一个元素,并返回跨过的元素
在这里插入图片描述
对于ListIterator来说,成员变量next就理解为游标

private class ListItr implements ListIterator<E> {
        private Node<E> lastReturned;
        private Node<E> next;
        private int nextIndex;
        private int expectedModCount = modCount;
        ....
}

当调用next()的时候,游标会向后移动一位,跨过一个元素,而这个跨过的元素会作为返回值返回

public E next() {
            checkForComodification();
            if (!hasNext())
                throw new NoSuchElementException();

            lastReturned = next;
            next = next.next;
            nextIndex++;
            return lastReturned.item;
        }

调用previous()会向前移动一位,返回的还是刚刚越过的元素,所以我先调用next(),越过一个元素,在调用previous()返回到刚刚位置,迭代器拿到的都是同一个(他们越过的元素)

 public E previous() {
            checkForComodification();
            if (!hasPrevious())
                throw new NoSuchElementException();

            lastReturned = next = (next == null) ? last : next.prev;
            nextIndex--;
            return lastReturned.item;
        }

而对于所以的remove()方法来说,都是要先调用next()方法(previous()),再调用remove()方法,因为remove()方法是对上一个返回的元素进行操作,而再调用remove()后会把lastReturned置为null

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

ListIterator

private class ListItr implements ListIterator<E> {
        private Node<E> lastReturned;
        private Node<E> next;
        private int nextIndex;
        private int expectedModCount = modCount;

        ListItr(int index) {
            // assert isPositionIndex(index);
            next = (index == size) ? null : node(index);
            nextIndex = index;
        }

        public boolean hasNext() {
            return nextIndex < size;
        }

        public E next() {
            checkForComodification();
            if (!hasNext())
                throw new NoSuchElementException();

            lastReturned = next;
            next = next.next;
            nextIndex++;
            return lastReturned.item;
        }

        public boolean hasPrevious() {
            return nextIndex > 0;
        }

        public E previous() {
            checkForComodification();
            if (!hasPrevious())
                throw new NoSuchElementException();

            lastReturned = next = (next == null) ? last : next.prev;
            nextIndex--;
            return lastReturned.item;
        }

        public int nextIndex() {
            return nextIndex;
        }

        public int previousIndex() {
            return nextIndex - 1;
        }

        public void remove() {
            //remove操作的都是lastReturned上一个返回的
            checkForComodification();
            //必须先迭代才能remove
            if(lastReturned == null){
                throw new IllegalStateException();
            }
            //接下来又要分两种情况
            //①向前迭代后删除 这个时候lastReturned == next,我们需要把next重新赋值
            //②向后迭代后
            Node<E> lastNext = lastReturned.next;
            unlink(lastReturned);
            if(lastReturned == next){
                next = lastNext;
            }else {
                nextIndex--;
            }
            lastReturned = null;
            expectedModCount++;
        }

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

    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值