码迷,mamicode.com
首页 > 编程语言 > 详细

java源码笔记-----AbstractList

时间:2018-05-11 17:21:43      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:lis   nbsp   expand   default   state   throw   lin   object   for   

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

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

 

java源码笔记-----AbstractList

标签:lis   nbsp   expand   default   state   throw   lin   object   for   

原文地址:https://www.cnblogs.com/goblinsenpai/p/9025210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!