java源码品读(10)— AbstractSequentialList

之前看了ArrayList的一些内容,而说起ArrayList就会想起另一个比较常用的类:LinkedList。

public class LinkedList<E> extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

LinkedList继承自AbstractSequentialList,AbstractSequentialList又继承AbstractList,AbstractList之前已经读过(这个是链接AbstractList),所以今天就看一下AbstractSequentialList的相关内容。

AbstractSequentialList中多出了一个词:Sequential,顾名思义,它的实现类是连续的、有顺序的list。在类的文档注释中,作者也进行了说明,并给出了相应的使用方式。

 // 如果想要使用这个类,只需要实现listIterator和size方法
 - To implement a list the programmer needs only to extend this class and
 - provide implementations for the <tt>listIterator</tt> and <tt>size</tt>

 //想要得到一个不可修改的list,那么只需要实现list迭代器中的hasNext、next、hasPrevious
 //、previous和index方法。
 - methods.  For an unmodifiable list, the programmer need only implement the
 - list iterator's <tt>hasNext</tt>, <tt>next</tt>, <tt>hasPrevious</tt>,
 - <tt>previous</tt> and <tt>index</tt> methods.<p>

 //如果想要一个可修改的list,那么还需要额外实现list迭代器的set方法
 - For a modifiable list the programmer should additionally implement the list
 - iterator's <tt>set</tt> method.  For a variable-size list the programmer
 //如果想要一个长度可变的list,那么还需要额外实现list迭代器的add和remove方法
 - should additionally implement the list iterator's <tt>remove</tt> and
 - <tt>add</tt> methods.<p>
 //当然也需要提供一个无参和集合的构造器
 - The programmer should generally provide a void (no argument) and collection
 - constructor, as per the recommendation in the <tt>Collection</tt> interface
 - specification.<p>

接下来看类中的相关内容:AbstractSequentialList类中提供了一个空参的构造器和几个重写的方法

  • public E get(int index)

根据下标去除相应的元素,有可能抛出下标越界异常(IndexOutOfBoundsException)

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)
    给指定下标的位置赋值,返回原来的数据。可能抛出的异常相对较多。
    /**
     * @throws UnsupportedOperationException 该集合不支持set操作是抛出
     * @throws ClassCastException            参数元素类型跟集合指定泛型类型不一致
     * @throws NullPointerException          集合为空
     * @throws IllegalArgumentException      参数非法
     * @throws IndexOutOfBoundsException     指定下表越界
     * /
  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)
  • public boolean addAll(int index, Collection<? extends E> c)

add方法在指定位置增加元素,addAll方法在指定位置之后加入参数集合的所有元素,可能抛出的异常同set方法一致

public void add(int index, E element) {
  try {
        listIterator(index).add(element);
    } 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 E remove(int index)

移除指定位置的元素,返回之前的元素。抛出的异常:集合不支持remove操作时的UnsupportedOperationException异常和下表越界IndexOutOfBoundsException异常

    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 Iterator<E> iterator() {
        return listIterator();
    }

    public abstract ListIterator<E> listIterator(int index);

所以我们能够看到所有的操作都是跟迭代器关的,这也就是为什么上面类的注释中作者说想要简单使用这个集合,只需要实现listIterator和size方法的原因。

猜你喜欢

转载自blog.csdn.net/m0_37664906/article/details/80923891