AbstractList源码解析

AbstractList源码解析

1. AbstractList抽象类继承自AbstracCollection,实现了List接口,用于 给List提供了一个骨架实现。下面我们逐个讨论其方法。
2.   
public boolean add(E e) {
        add(size(), e);
        return true;
    }
该方法是进行数据的添加,添加的过程中 调用了add(index,e)方法,并且这个方法只是在函数体中抛出了异常,并不进行任何处理,而真正这个函数的功能将交由继承它的子类来进行实现。相同情况还有set方法。住该类中的get方法是一个抽象方法。
3.
public int indexOf(Object o) {
        ListIterator<E> it = listIterator();
        if (o==null) {
            while (it.hasNext())
                if (it.next()==null)
                    return it.previousIndex();
        } else {
            while (it.hasNext())
                if (o.equals(it.next()))
                    return it.previousIndex();
        }
        return -1;
    }
此类可以用于查找list中是否存在元素o,(注意:list可以存在null,并且list中可以存储相同的数据),并返回其下标。
4.
 public int lastIndexOf(Object o) {
        ListIterator<E> it = listIterator(size());
        if (o==null) {
            while (it.hasPrevious())
                if (it.previous()==null)
                    return it.nextIndex();
        } else {
            while (it.hasPrevious())
                if (o.equals(it.previous()))
                    return it.nextIndex();
        }
        return -1;
    }
注意这里使用了ListIterator来对list进行遍历,并且ListIterator能够支持双向遍历。
5.
 public void clear() {
        removeRange(0, size());
    }
此方法调用了removeRange(fromIndex,toIndex)方法,removeRange方法使用ListIterator对数据进行遍历,并调用remove方法进行删除。
6.
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;
    }
此方法用于进行数据的赋值,赋值的对象为collection,首先此方法会调用rangeCheckForAdd(index)方法进行相关的下标检测,如果下标小于0或者是下边大于size的话,抛出outOfIndexException异常。最后进行相关的复制操作。
7.
 private class Itr implements Iterator<E> {
        /**
         * Index of element to be returned by subsequent call to next.
         */
        int cursor = 0;

        /**
         * Index of element returned by most recent call to next or
         * previous.  Reset to -1 if this element is deleted by a call
         * to remove.
         */
        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;
                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();
        }
    }
私有的内部类Itr,实现了Iterator接口。重写了接口中的方法,主要就是对三个参数进行操作。
8.
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());
    }
此方法用于判断两个list是否相等,首先判断的是两个list的地址是否相等(使用==,判断两个list的地址),若相等返回true。第二个if判断两个o对象是不是list对象(包含其子对象)。第三个判断语句判断的是list中的元素是否相等。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值