AbstractList抽象类

package java.util;
//顺序存取的list 的抽象类,继承AbstractList(随机存取)
//顺序存取,直接通过index来存取数据
public abstract class AbstractSequentialList<E> extends AbstractList<E> {

    protected AbstractSequentialList(){
    }

    public E get(int index){
        try {
            return listIterator(index).next();
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: " + index);
        }
    }

    public E set(int index, E element){
        try {
            ListIterator<E> e = listIterator(index);
            E oldVal = e.next();
            e.set(element);
            return oldVal;
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: " + index);
        }
    }

    public void add(int index, E element){
        try {
            listIterator(index).add(element);
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: " + index);
        }
    }

    public E remove(int index){
        try {
            ListIterator<E> e = listIterator(index);
            E outCast = e.next();
            e.remove();
            return outCast;
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: " + index);
        }
    }

    // Bulk Operations
    public boolean addAll(int index, Collection<? extends E> c){
        try {
            boolean modified = false;
            ListIterator<E> e1 = listIterator(index);
            Iterator<? extends E> e2 = c.iterator();
            while (e2.hasNext()) {
                e1.add(e2.next());
                modified = true;
            }
            return modified;
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: " + index);
        }
    }


    // Iterators
    public Iterator<E> iterator(){
        return listIterator();
    }
    public abstract ListIterator<E> listIterator(int index);
}
Java之uitil包源码阅读 文章被收录于专栏

源码阅读是程序员必不可少的,本专栏记录本人阅读java源码笔记,与阅读此专栏的同道共同进步。 本专栏以字典序对源码进行阅读注释,主要工作:1.去除源码中冗长的英语注释,用简短的注释代替;2.对关键语句和算法予以说明。3.概要性描述。

全部评论

相关推荐

只有一个苍穹外卖外加正在看黑马点评,可以找小厂实习吗,还有我的简历有什么大问题吗
Java抽象小篮子:感觉有点熟悉,问题1是学历,2是没实习经历,3是专业技能写得太少太少了(怎么写可以看我置顶帖),4是仅这一个项目找实习不够看。拷打完毕,简历怎么写可以看我置顶帖子
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务