java源码笔记-----AbstractList

注:代码中的注释是我的个人理解,如果有误还请指出。

AbstractList

add操作将元素添加到列表末尾。

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

remove操作将指定元素移除列表,通过Iterator的remove方法实现。

public boolean remove(Object o) {
        Iterator<E> it = iterator();
        if (o==null) {
            while (it.hasNext()) {
                if (it.next()==null) {
                    it.remove();//调用iterator的remove方法
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (o.equals(it.next())) {
                    it.remove();
                    return true;
                }
            }
        }
        return false;
    }
/*==========Iterator的remove方法============*/
/*This method can be called only once per call to {@link #next}.*/
/*此方法只能在next()方法调用后调用,因为当当前指针指向的内存中没有元素时会抛出IllegalStateException*/default void remove() {
        throw new UnsupportedOperationException("remove");
    }

clear操作通过调用iterator的remove移除列表中的所有元素

 
 public void clear() {
        Iterator<E> it = iterator();
        while (it.hasNext()) {
            it.next();//先调用next
            it.remove();//再移除
        }
    }

AbstractList.Itr

next()

     public E next() {
            checkForComodification();
            try {
                int i = cursor;//获取指针
                E next = get(i);//获取指针对应的元素
                lastRet = i;//最近调用过next()的元素的index
                cursor = i + 1;//指针右移
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

remove()


    public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();
​
            try {
                AbstractList.this.remove(lastRet);//移除上一次next()或者previous()的元素,列表后面的元素依次左移
                if (lastRet < cursor)//如果lastRet小于cursor则说明是next()
                    cursor--;//指针左移
                lastRet = -1;//重置为-1
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }
 

AbstractList.ListItr

previous()

public E previous() {
            checkForComodification();
            try {
                int i = cursor - 1;//获取当前指针的上一个元素的index
                E previous = get(i);//获取该元素
                lastRet = cursor = i;//cursor指针左移,将lastRet设置为i
                return previous;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

add()

 
    public void add(E e) {
            checkForComodification();
​
            try {
                int i = cursor;//获取当前指针所处的位置
                AbstractList.this.add(i, e);//插入到i处
                lastRet = -1;
                cursor = i + 1;//指针右移,以保持指针所指的元素不变
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

猜你喜欢

转载自www.cnblogs.com/goblinsenpai/p/9025210.html