6.AbstractList

继承了 AbstractCollection 实现了 List

extends AbstractCollection implements List
1.Itr内部类

内部类迭代器

private class Itr implements Iterator {
       
        int cursor = 0;//cursor对于每次调用next方法就增加一次。并且将上一个cursor的值传给lastRet(用于可能的删除操作)

     
        int lastRet = -1;

        /**
         * The modCount value that the iterator believes that the backing
         * List should have.  If this expectation is violated, the iterator
         * has detected concurrent modification.
         */
        int expectedModCount = modCount;

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

        public E next() {//遍历操作
            checkForComodification();
            try {
                int i = cursor;//每次调用next方法就增加一次类似于下标位置的作用
                E next = get(i);//元素值
                lastRet = i;//记录当前位置
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

        public void remove() {//删除前位元素
            if (lastRet < 0)//当前位置
                throw new IllegalStateException();
            checkForComodification();

            try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }

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

首先对于每一个迭代器,保存了三个变量。一个是cursor,一个是lastRet,一个是expectedModCount。
cursor对于每次调用next方法就增加一次。并且将上一个cursor的值传给lastRet(用于可能的删除操作),将刚刚跳过的第i个元素作为返回值。这里得到刚刚跳过的元素的方法是使用了AbstractList类的get方法。

对于如何判断迭代到尾的hasNext方法,采用的是比较cursor的值和size的大小。如果两者相等则说明迭代到尾了。

2.ListItr 内部类

继承自Itr 实现了ListIterator

private class ListItr extends Itr implements ListIterator {
        ListItr(int index) {
            cursor = index;
        }

        public boolean hasPrevious() {//判断有没有前一个元素
            return cursor != 0;
        }

        public E previous() {//获取前一个元素
            checkForComodification();
            try {
                int i = cursor - 1;
                E previous = get(i);
                lastRet = cursor = i;
                return previous;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

        public int nextIndex() {
            return cursor;
        }

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

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

            try {
                AbstractList.this.set(lastRet, e);
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        public void add(E e) {//新增元素
            checkForComodification();

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

你可能感兴趣的:(6.AbstractList)