您的位置:首页 > 编程语言 > Java开发

Java源码阅读之ArrayList(4)

2018-01-24 11:04 363 查看
    /**
     * AbstractList.ListItr的优化版本
     */
    private class ListItr extends Itr implements ListIterator<E> {
        
        /**
         * 构造函数,必须指定初始句柄位置
         */
           ListItr(int index) {
            super();
            cursor = index;
        }
  
         // 检查当前句柄之前是否还存在元素
        public boolean hasPrevious() {
            return cursor != 0;
        }

           // 获取当前句柄的下一个元素的位置
        public int nextIndex() {
            return cursor;
        }

           // 获取当前句柄的前一个元素的位置
        public int previousIndex() {
            return cursor - 1;
        }

           // 返回句柄的前一个元素
        @SuppressWarnings("unchecked")
        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];
        }

// 替换列表中上次迭代器访问的元素
        public void set(E e) {
            if (lastRet < 0)           // 至少使用迭代器进行迭代过一次之后才允许使用迭代器进行set操作
                throw new IllegalStateException();
            checkForComodification();  // 线程安全检查(是否有其他线程对当前列表进行了修改)
   
try {
                ArrayList.this.set(lastRet, e);
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
// 在当前句柄位置添加元素
        public void add(E e) {
            checkForComodification(); // 线程安全检查(是否有其他线程对当前列表进行了修改)

try {
                int i = cursor;
                ArrayList.this.add(i, e);
                cursor = i + 1; // 句柄后移
                lastRet = -1;  // 重置 lastRet,必须再次迭代过后才能对执行set
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
}
    }

    /**
     * 返回列表的子列表
     * 这里千万要注意,返回的子列表是原来列表的一部分,也就是对子列表的修改会影响的原列表
     */
    public List<E> subList(int fromIndex, int toIndex) {
        subListRangeCheck(fromIndex, toIndex, size); // 参数合法性检查
        return new SubList(this, 0, fromIndex, toIndex);
    }

  // 参数合法性检查 
    static void subListRangeCheck(int fromIndex, int toIndex, int size) {
        if (fromIndex < 0)
            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
        if (toIndex > size)
            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
        if (fromIndex > toIndex)
            throw new IllegalArgumentException("fromIndex(" + fromIndex +
                                               ") > toIndex(" + toIndex + ")");
    }

// 子列表实现类
    private class SubList extends AbstractList<E> implements RandomAccess {
        private final AbstractList<E> parent;   // 原列表指针
        private final int parentOffset;    // 原列表偏移量,即当前子列表是从原列表的什么位置开始的
        private final int offset; // 子列表的偏移量,是在原列表的基础上进行计算的,所以大于等于原列表的偏移量
        int size;                 // 子列表大小

  // 构造函数
        SubList(AbstractList<E> parent,
                int offset, int fromIndex, int toIndex) {
            this.parent = parent;
            this.parentOffset = fromIndex;
            this.offset = offset + fromIndex;
            this.size = toIndex - fromIndex;
            this.modCount = ArrayList.this.modCount;
        }

  // 替换子列表index位置的元素
        public E set(int index, E e) {
            rangeCheck(index);            // 参数合法性检查
            checkForComodification();     // 线程安全检查(是否有其他线程对子列表进行了修改)
            E oldValue = ArrayList.this.elementData(offset + index);
 
bca2
          ArrayList.this.elementData[offset + index] = e;
            return oldValue;
        }

  // 获取子列表中index位置的元素
        public E get(int index) {
            rangeCheck(index);            // 参数合法性检查
            checkForComodification();     // 线程安全检查(是否有其他线程对子列表进行了修改) 
            return ArrayList.this.elementData(offset + index);
        }

  // 获取子列表的大小
        public int size() {
            checkForComodification();
            return this.size;
        }
 
  // 在index位置添加元素
        public void add(int index, E e) {
            rangeCheckForAdd(index);      // 参数合法性检查
            checkForComodification();     // 线程安全检查(是否有其他线程对子列表进行了修改) 
            parent.add(parentOffset + index, e);
            this.modCount = parent.modCount;
            this.size++;
        }

  // 将index位置的元素移除
        public E remove(int index) {
            rangeCheck(index);            // 参数合法性检查
            checkForComodification();     // 线程安全检查(是否有其他线程对子列表进行了修改)
            E result = parent.remove(parentOffset + index);
            this.modCount = parent.modCount;
            this.size--;
            return result;
        }

  // 将从fromIndex到toIndex的元素全部移除
        protected void removeRange(int fromIndex, int toIndex) {
         checkForComodification();     // 线程安全检查(是否有其他线程对子列表进行了修改)
            parent.removeRange(parentOffset + fromIndex,
                               parentOffset + toIndex);
            this.modCount = parent.modCount;
            this.size -= toIndex - fromIndex;
        }

  // 将集合c中的元素全部加入到当前列表
        public boolean addAll(Collection<? extends E> c) {
            return addAll(this.size, c);
        }

  // 将集合c中的元素全部加入到当前列表的index位置之后
        public boolean addAll(int index, Collection<? extends E> c) {
            rangeCheckForAdd(index);     // 参数合法性检查
            int cSize = c.size();
            if (cSize==0)
                return false;

            checkForComodification();    // 线程安全检查(是否有其他线程对子列表进行了修改)
            parent.addAll(parentOffset + index, c);
            this.modCount = parent.modCount;
            this.size += cSize;
            return true;
        }

  // 获取当前子列表的迭代器
        public Iterator<E> iterator() {
            return listIterator();
        }

  // 返回从index位置开始的子列表的迭代器
        public ListIterator<E> listIterator(final int index) {
            checkForComodification();
            rangeCheckForAdd(index);
            final int offset = this.offset;

    // 实现匿名内部类,方法解析在上方,就不赘述了
            return new ListIterator<E>() {
                int cursor = index;
                int lastRet = -1;
                int expectedModCount = ArrayList.this.modCount;

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

                @SuppressWarnings("unchecked")
                public E next() {
                    checkForComodification();
                    int i = cursor;
                    if (i >= SubList.this.size)
                        throw new NoSuchElementException();
                    Object[] elementData = ArrayList.this.elementData;
                    if (offset + i >= elementData.length)
                        throw new ConcurrentModificationException();
                    cursor = i + 1;
                    return (E) elementData[offset + (lastRet = i)];
                }

                public boolean hasPrevious() {
                    return cursor != 0;
                }

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

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

                public int nextIndex() {
                    return cursor;
                }

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

                public void remove() {
                    if (lastRet < 0)
                        throw new IllegalStateException();
                    checkForComodification();

                    try {
                        SubList.this.remove(lastRet);
                        cursor = lastRet;
                        lastRet = -1;
                        expectedModCount = ArrayList.this.modCount;
                    } catch (IndexOutOfBoundsException ex) {
                        throw new ConcurrentModificationException();
                    }
                }

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

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

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

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

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

  // 获取当前子列表的子列表
        public List<E> subList(int fromIndex, int toIndex) {
            subListRangeCheck(fromIndex, toIndex, size);
            return new SubList(this, offset, fromIndex, toIndex);
        }

  // 参数检查
        private void rangeCheck(int index) {
            if (index < 0 || index >= this.size)
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }

  // 添加元素参数检查
        private void rangeCheckForAdd(int index) {
            if (index < 0 || index > this.size)
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }

  // 数组越界异常消息
        private String outOfBoundsMsg(int index) {
            return "Index: "+index+", Size: "+this.size;
        }

  // 线程安全检查
        private void checkForComodification() {
            if (ArrayList.this.modCount != this.modCount)
                throw new ConcurrentModificationException();
        }

  // 返回当前子列表的分割器
        public Spliterator<E> spliterator() {
            checkForComodification();
            return new ArrayListSpliterator<E>(ArrayList.this, offset,
                                               offset + this.size, this.modCount);
        }
    }

  // forEach方法,加强版的for循环,遍历集合
    @Override
    public void forEach(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        final int expectedModCount = modCount;
        @SuppressWarnings("unchecked")
        final E[] elementData = (E[]) this.elementData;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            action.accept(elementData[i]);
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
    }

    /**
     * 返回当前列表的分割器
     */
    @Override
    public Spliterator<E> spliterator() {
        return new ArrayListSpliterator<>(this, 0, -1, 0);
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: