zoukankan      html  css  js  c++  java
  • AbstractList

    概述

            此类提供 List 接口的骨干实现,以最大限度地减少实现“随机访问”数据存储(如数组)支持的该接口所需的工作。对于连续的访问数据(如链表),应优先使用 AbstractSequentialList,而不是此类。

    定义

    public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

    构造函数

        protected AbstractList() {
        }
    View Code

     iterator()

        public Iterator<E> iterator() {
            return new Itr();
        }
    View Code

    上面用到了Ite

      一个内部类
      private class Itr implements Iterator<E> {
       调用next时,返回元素的下标     
            int cursor = 0;
     最近访问元素下标(next或previo),如果元素被删返回-1
            int lastRet = -1;
    期望改变数,当迭代时检测在此期间元数组是否改变,fast-fail的实现基础
            int expectedModCount = modCount;
    
            public boolean hasNext() {
                return cursor != size();
            }
    
            public E next() {
                checkForComodification();
                try {
                    int i = cursor;
                    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();
                 调用外部remove方法
                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();
            }
        }    
    View Code

    ListIterator<E>

        public ListIterator<E> listIterator() {
            return listIterator(0);
        }
    View Code

    上面用到了 listIterator(0);

        public ListIterator<E> listIterator(final int index) {
            rangeCheckForAdd(index);
    
            return new ListItr(index);
        }
    View Code

     ListIt

     private class ListItr extends Itr implements ListIterator<E> {
            ListItr(int index) {
                cursor = index;
            }
        
            public boolean hasPrevious() {
                return cursor != 0;
            }
    
            public E previous() {
                checkForComodification();
                try {
                    int i = cursor - 1;// cursor从Itr继承的字段,表示下一个next返回元素下标,
                    E previous = get(i);
                    lastRet = cursor = i; // 执行previous  标志前移
                    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();
                }
            }
        }
    View Code
  • 相关阅读:
    xhtml+css (网站重构)
    一个典型的代码走查检查单
    谈谈单位时间内投票次数限制
    .NET性能优化方面的总结
    IE6.0、IE7.0 与 FireFox CSS
    vue3中使用 aggrid 表格组件
    基于predis高并发情况下实现频率控制的函数
    Redis之Centos下使用redis
    SVN的安装和使用
    Git配置教程
  • 原文地址:https://www.cnblogs.com/whesuanfa/p/7358560.html
Copyright © 2011-2022 走看看