容器 AbstractList 源码解析

5 篇文章 0 订阅

AbstractList 源码解析

容器 AbstractCollection源码解析

//气不气,他还继承了AbstractCollection,怎么就没完没了呢;

//(容器 AbstractCollection源码解析) https://blog.csdn.net/xiey94/article/details/98046498

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    //构造器
    protected AbstractList() {
    }

    //添加一个元素到容器中
    public boolean add(E e) {
        add(size(), e);
        return true;
    }

    //根据索引获取元素
    abstract public E get(int index);
    
    //指定索引,设置值,子类实现
    public E set(int index, E element) {
        throw new UnsupportedOperationException();
    }

    //指定索引添加值,子类实现
    public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }

    //根据索引删除元素,子类实现
    public E remove(int index) {
        throw new UnsupportedOperationException();
    }
    
    //返回指定元素在当前容器中的索引值
    public int indexOf(Object o) {
        ListIterator<E> it = listIterator();
        if (o==null) {
            while (it.hasNext())
                if (it.next()==null)
                    //这里为何要指向上一个索引,因为当执行next的时候,索引往下走了
                    return it.previousIndex();
        } else {
            while (it.hasNext())
                if (o.equals(it.next()))
                    //同理
                    return it.previousIndex();
        }
        //没找到返回-1
        return -1;
    }

    //查找当前元素在容器中的最后一个相匹配的索引
    public int lastIndexOf(Object o) {
        //传入了size(),这就是迭代器游标的值
        ListIterator<E> it = listIterator(size());
        if (o==null) {
            //判断是否有上一个元素
            while (it.hasPrevious())
                if (it.previous()==null)
                    //判断上一个元素是否为null的时候,游标又往前走了一步,所以这里返回的是下一个的索引位置
                    return it.nextIndex();
        } else {
            while (it.hasPrevious())
                if (o.equals(it.previous()))
                    //同理
                    return it.nextIndex();
        }
        //没找到返回-1
        return -1;
    }

    //清理容器中的所有元素
    public void clear() {
        //该方法内部实现就是从第一个参数的位置迭代移除到第二个参数的位置
        removeRange(0, size());
    }

    //将指定容器中的元素加入到容器中,从指定索引开始
    public boolean addAll(int index, Collection<? extends E> c) {
        //边界值检查    
        rangeCheckForAdd(index);
        //只要加入一个就算修改过当前容器
        boolean modified = false;
        for (E e : c) {
            //循环添加
            add(index++, e);
            modified = true;
        }
        return modified;
    }

    //实例化一个迭代器
    public Iterator<E> iterator() {
        return new Itr();
    }
    //实例化一个迭代器,游标完全从第一个开始
    public ListIterator<E> listIterator() {
        return listIterator(0);
    }

    //实例化一个,指定游标位置
    public ListIterator<E> listIterator(final int index) {
        //边界值检查
        rangeCheckForAdd(index);
        //实例化一个迭代器
        return new ListItr(index);
    }
    
    //迭代器
    private class Itr implements Iterator<E> {
        
        //游标 从0开始
        int cursor = 0;

        //游标的上一位,从-1开始,紧跟着游标
        int lastRet = -1;

        //防止多线程操作导致的异常
        int expectedModCount = modCount;
        //判断是否有下一个元素;size是从1开始计数的,游标cursor是从0开始游走的
        public boolean hasNext() {
            //只要游标不等于size就说明还有下一个,一旦等于,就说明没了
            return cursor != size();
        }
        
        //获取下一个元素
        public E next() {
            //对expectedModCount = modCount的大小比较
            checkForComodification();
            try {
                //获取当前的游标值
                int i = cursor;
                //获取对应游标的值
                E next = get(i);
                //将lastRet指向上一个游标所在位置
                lastRet = i;
                //游标做+1操作
                cursor = i + 1;
                //同时返回当前的元素值
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }
        
        //移除
        public void remove() {
            //一次remove后lastRet为-1,这里可以防止remove二次连续操作
            if (lastRet < 0)
                throw new IllegalStateException();
            //对expectedModCount = modCount的大小比较
            checkForComodification();

            try {
                //做remove操作 子类实现,用lastRet做索引,则说明lastRet起码要等于0,也就是游标起码要等于1;
                //这里为何要用lastRet做索引,而不用cursor做索引?
                //因为按照我们常规的做法就是先判断是否有下一个元素,用到了nex方法,此时迭代器的游标往下走了一位,此时要是remove游标所在索引,就移除了下一个
                //在ArrayList中的实现就是,数组将索引以后的元素整体往前移一位,将数组最后的一个元素置为null
                AbstractList.this.remove(lastRet);
                //做安全检查
                if (lastRet < cursor)
                    //游标后退一位
                    cursor--;
                //将lastRet置为-1
                lastRet = -1;
                //remove在子类中操作中,modCount做了+1操作
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }
        
        //有两个线程(线程A,线程B),其中线程A负责遍历list、线程B修改list。 
        //-线程A在遍历list过程的某个时候(此时expectedModCount=modCount=N),线程启动,
        //同时线程B增加一个元素,这是modCount的值发生改变(modCount + 1 = N + 1)。 线程A继续遍历执行next方法时,
        //通告checkForComodification方法发现expectedModCount =N,而modCount = N + 1,两者不等,
        //这时就抛出ConcurrentModificationException异常,从而产生fail-fast机制。
        //https://blog.csdn.net/weixin_40254498/article/details/81386920
        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

    //迭代器 实现了ListIterator[里面增加了是否有上一个,下一个,上一个的索引,下一个的索引]
    private class ListItr extends Itr implements ListIterator<E> {
        //带参构造,指定游标
        ListItr(int index) {
            cursor = index;
        }
        
        //判断是否还有上一个,如果 游标==0,则表示没有了;因为游标一旦开始获取,就是从1开始的
        public boolean hasPrevious() {
            return cursor != 0;
        }
        
        //获取上一个元素
        public E previous() {
            checkForComodification();
            try {
                //游标往前走一位
                int i = cursor - 1;
                //获取索引对应的元素
                E previous = get(i);
                //lastRet赋值,因为是往前走的,所以这里游标往前走一位
                lastRet = cursor = i;
                return previous;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }
        //下一个的索引,就是游标对应的索引
        public int nextIndex() {
            return cursor;
        }
        //上一个,就是游标的索引-1
        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();
            }
        }
    }

    //名字叫做截取List,返回了一个实现类,这个实现类中记录了fromIndex,在他的内部叫做offset,同时还记住了size=toIndex-fromIndex;不管做啥操作都始终把这两个值带着,所以起到了截取的作用,其实他的内部还是原来的容器,做不过他的索引0对应着offset;
    public List<E> subList(int fromIndex, int toIndex) {
        return (this instanceof RandomAccess ?
                new RandomAccessSubList<>(this, fromIndex, toIndex) :
                new SubList<>(this, fromIndex, toIndex));
    }

    //比较
    public boolean equals(Object o) {
        //比较对象
        if (o == this)
            return true;
        if (!(o instanceof List))//比较类型
            return false;
        
        //迭代比较值
        ListIterator<E> e1 = listIterator();
        ListIterator<?> e2 = ((List<?>) o).listIterator();
        while (e1.hasNext() && e2.hasNext()) {
            E o1 = e1.next();
            Object o2 = e2.next();
            //做值比较
            if (!(o1==null ? o2==null : o1.equals(o2)))
                return false;
        }
        return !(e1.hasNext() || e2.hasNext());
    }

    //hsshCode
    public int hashCode() {
        int hashCode = 1;
        for (E e : this)
            hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
        return hashCode;
    }

    //指定范围移除
    protected void removeRange(int fromIndex, int toIndex) {
        ListIterator<E> it = listIterator(fromIndex);
        for (int i=0, n=toIndex-fromIndex; i<n; i++) {
            it.next();
            it.remove();
        }
    }
    
    //操作次数统计
    protected transient int modCount = 0;
    
    //边界值检查
    private void rangeCheckForAdd(int index) {
        if (index < 0 || index > size())
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }
    
    //输出信息-边界值检查
    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size();
    }
}

//截取List,只是做显示截取,本质还是原来的那个容器
class SubList<E> extends AbstractList<E> {
    private final AbstractList<E> l;
    private final int offset;
    private int size;

    SubList(AbstractList<E> list, int fromIndex, int toIndex) {
        if (fromIndex < 0)
            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
        if (toIndex > list.size())
            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
        if (fromIndex > toIndex)
            throw new IllegalArgumentException("fromIndex(" + fromIndex +
                                               ") > toIndex(" + toIndex + ")");
        //保留这个list
        l = list;
        //记住差值
        offset = fromIndex;
        //记住大小
        size = toIndex - fromIndex;
        //记住操作次数
        this.modCount = l.modCount;
    }

    //完全就是代理模式,内部调用之前容器对应的方法,记住偏差值
    public E set(int index, E element) {
        rangeCheck(index);
        checkForComodification();
        return l.set(index+offset, element);
    }
    
    //完全就是代理模式,内部调用之前容器对应的方法,记住偏差值
    public E get(int index) {
        rangeCheck(index);
        checkForComodification();
        return l.get(index+offset);
    }
    
    //返回大小
    public int size() {
        checkForComodification();
        return size;
    }

    //完全就是代理模式,内部调用之前容器对应的方法,记住偏差值
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        checkForComodification();
        l.add(index+offset, element);
        this.modCount = l.modCount;
        size++;
    }
    
    //完全就是代理模式,内部调用之前容器对应的方法,记住偏差值
    public E remove(int index) {
        rangeCheck(index);
        checkForComodification();
        E result = l.remove(index+offset);
        this.modCount = l.modCount;
        size--;
        return result;
    }
    
    //完全就是代理模式,内部调用之前容器对应的方法,记住偏差值
    protected void removeRange(int fromIndex, int toIndex) {
        checkForComodification();
        l.removeRange(fromIndex+offset, toIndex+offset);
        this.modCount = l.modCount;
        size -= (toIndex-fromIndex);
    }

    public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }
    
    //完全就是代理模式,内部调用之前容器对应的方法,记住偏差值
    public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);
        int cSize = c.size();
        if (cSize==0)
            return false;

        checkForComodification();
        l.addAll(offset+index, c);
        this.modCount = l.modCount;
        size += cSize;
        return true;
    }

    //迭代器
    public Iterator<E> iterator() {
        return listIterator();
    }

    //迭代器 完全就是代理模式,内部调用之前容器对应的方法,记住偏差值
    public ListIterator<E> listIterator(final int index) {
        checkForComodification();
        rangeCheckForAdd(index);

        return new ListIterator<E>() {
            private final ListIterator<E> i = l.listIterator(index+offset);

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

            public E next() {
                if (hasNext())
                    return i.next();
                else
                    throw new NoSuchElementException();
            }

            public boolean hasPrevious() {
                return previousIndex() >= 0;
            }

            public E previous() {
                if (hasPrevious())
                    return i.previous();
                else
                    throw new NoSuchElementException();
            }

            public int nextIndex() {
                return i.nextIndex() - offset;
            }

            public int previousIndex() {
                return i.previousIndex() - offset;
            }

            public void remove() {
                i.remove();
                SubList.this.modCount = l.modCount;
                size--;
            }

            public void set(E e) {
                i.set(e);
            }

            public void add(E e) {
                i.add(e);
                SubList.this.modCount = l.modCount;
                size++;
            }
        };
    }

    public List<E> subList(int fromIndex, int toIndex) {
        return new SubList<>(this, fromIndex, toIndex);
    }

    private void rangeCheck(int index) {
        if (index < 0 || index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private void rangeCheckForAdd(int index) {
        if (index < 0 || index > size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size;
    }

    private void checkForComodification() {
        if (this.modCount != l.modCount)
            throw new ConcurrentModificationException();
    }
}

//只是多加了一个RandomAccess接口
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
    RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
        super(list, fromIndex, toIndex);
    }

    public List<E> subList(int fromIndex, int toIndex) {
        return new RandomAccessSubList<>(this, fromIndex, toIndex);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值