14java源码解析-AbstractList

其他 源码解析 https://blog.csdn.net/qq_32726809/article/category/8035214 

类的声明

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
  • extends AbstractCollection<E> 继承抽象类
  • 接口已经将 Collection 大体的骨架写了出来
  • 如果要实现不可变集合,只需要继承这个类,实现iterator 和 size两个方法
  • 如果需要实现可变集合,需要继承这个类,需要实现add方法,并且迭代器的remove方法也要实现
  • implements List 实现list接口
  • 有序集合,能够根据索引进行控制

2常用方法

由于是继承list接口和实现抽象类,所以很多方法以前都介绍过,这里只说必要的方法

2.1indexOf

 public int indexOf(Object o) {
        ListIterator<E> it = listIterator();/*-------1*/
        if (o==null) {
            while (it.hasNext())
                if (it.next()==null)
                    return it.previousIndex();/*-------2*/
        } else {
            while (it.hasNext())
                if (o.equals(it.next()))
                    return it.previousIndex();
        }
        return -1;
    }
  • 1处的意思是 列表迭代器,继承自 Iterator
  • 新增从后向前遍历hasPrevious(), previous(), previousIndex(),
  • 2返回当前索引

2.2iterator()

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

Itr 内部类源码

  • 这个是实现的 Iterator,实现了迭代器的 next,hasnext,remove方法
 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();
            }
        }

2.3listIterator

 public ListIterator<E> listIterator(final int index) {
        rangeCheckForAdd(index);

        return new ListItr(index);
    }

ListItr 内部类源码

  • 在继承Itr的基础上实现 ListIterator接口,使得多了 从尾部遍历的方法
   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;
                E previous = get(i);
                lastRet = cursor = i;
                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();
            }
        }
    }

subList

利用内部类调用

  public List<E> subList(int fromIndex, int toIndex) {
        return (this instanceof RandomAccess ?
                new RandomAccessSubList<>(this, fromIndex, toIndex) :
                new SubList<>(this, fromIndex, toIndex));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值