Java源码阅读之ArrayList(4)

    /**
     * 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);             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);     }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值