java.util.AbstractSequentialList

  1. 抽象類定義
//繼承了AbstractList類,該類的增刪查改全是通過迭代器做的,並沒有實際針對存儲數組做操作
public abstract class AbstractSequentialList<E> extends AbstractList<E>
  1. 構造方法
protected AbstractSequentialList() {
    }
  1. 抽象方法
public abstract ListIterator<E> listIterator(int index);  //返回該集合的迭代器
  1. 具體方法
//獲取指定位置的元素
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);
        }
    }

//在指定位置插入一個集合c,如果有插入就返回true
 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);
        }
    }

//返回該集合的迭代器,爲ListIterator對象
public Iterator<E> iterator() {
        return listIterator();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章