AbstractList源码读书笔记

AbstractList源码读书笔记

感觉自己java基础很不好很多东西只是了解,所以现在多看看源码望各位大佬给出指导。感谢感谢!
1.8版本新加的方法,打算等学习了1.8的新特性后再看。

先看AbstractList方法和关系结构图


abstract public E get(int index);

没有实现 get(),子类必须实现


public boolean add(E e) {
    add(size(), e);
    return true;
}

 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)
                return it.previousIndex();
    } else {
        while (it.hasNext())
            if (o.equals(it.next()))
                return it.previousIndex();
    }
    return -1;
}

判断对象o第一次出现的索引,若为null不为null,要是没找打返回-1


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;
}


//调用的函数
public ListIterator<E> listIterator(final int index) {
    //范围检查    
    rangeCheckForAdd(index);

    return new ListItr(index);
}

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

判断元素为null和非null 往前遍历查看,返回此列表中指定元素的最后一次出现的索引,如果此列表不包含元素,则返回-1。


public void clear() {
    removeRange(0, size());
}

//调用函数

protected void removeRange(int fromIndex, int toIndex) {
    ListIterator<E> it = listIterator(fromIndex);
    //删除fromIndex游标开始到toIndex的元素
    for (int i=0, n=toIndex-fromIndex; i<n; i++) {
        it.next();
        it.remove();
    }
}

清除集合中的所有元素。


public boolean addAll(int index, Collection<? extends E> c) {
    //检查index
    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);
}

两个迭代器

内部类Itr

 private class Itr implements Iterator<E> {
    //游标
    int cursor = 0;
    //最近一次或之前的调用返回的元素的索引。重置为-1,如果该元素被调用删除。
    int lastRet = -1;
    //用来判断并发修改问题
    int expectedModCount = modCount;

    public boolean hasNext() {
        return cursor != size();
    }

    public E next() {
        //每调用一次next()函数都会调用checkForComodification方法判断
        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;//重置为-1
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException e) {
            throw new ConcurrentModificationException();
        }
    }
    //此方法用来判断创建迭代对象的时候List的modCount与现在List的modCount是否一样,不一样的话就报ConcurrentModificationException异常
    final void checkForComodification() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
    }
}

内部类:ListItr

 private class ListItr extends Itr implements ListIterator<E> {
    ListItr(int index) {
        cursor = index;
    }

    public boolean hasPrevious() {
        return cursor != 0;
    }
    //往前
    public E previous() {
        //检查modCount和expectedModCount是否一致
        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;//add后不能调用remove方法
            cursor = i + 1;
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    }
}

本来看为什么没有remove方法 仔细一看原来继承了Itr。


 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());
}

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

equals和hashCode就跳过


//记录集合改变次数
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();
}

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

返回指定的fromIndex (含)和toIndex之间的列表部分的视图。


查看SubList类及其关系

具体方法就不介绍了,基本是建立在父类AbstractList基础上操作。

猜你喜欢

转载自blog.csdn.net/u013164931/article/details/79640715
今日推荐