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