Java AbstractSequentialList 抽象类

继承于 AbstractList ,提供了对数据元素的链式访问而不是随机访问。

源码展示

package java.util;

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


    /**
     * 批量添加
     */
    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);
        }
    }


    /**
     * 返回迭代器
     */
    public Iterator<E> iterator() {
        return listIterator();
    }

    /**
     * 返回迭代器
     */
    public abstract ListIterator<E> listIterator(int index);
}
原文地址:https://www.cnblogs.com/feiqiangsheng/p/14987767.html