Raj*_*Raj 12 java collections linked-list list arraylist
为什么LinkedList与ArrayList扩展AbstractList中的Java?
当我们想要在实现类中指定公共行为时,使用抽象类.
但是所有的方法AbstractList都被ArrayList和被覆盖了LinkedList.
那么扩展这个类有什么用呢?
san*_*hat 12
subList(int,int)方法不受既重写ArrayList和LinkedList,为此AbstractList提供了一个通用的实施
来自Java源码
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<E>(this, fromIndex, toIndex) :
new SubList<E>(this, fromIndex, toIndex));
}
Run Code Online (Sandbox Code Playgroud)
此外,还有其他方法不会覆盖toString()和iterator()
你可以从Here ,,, AbstractList获得答案
此类提供List接口的骨干实现,以最大限度地减少实现由"随机访问"数据存储(例如数组)支持的此接口所需的工作量.对于顺序访问数据(例如链接列表),应优先使用AbstractSequentialList,而不是此类.要实现不可修改的List,程序员只需要扩展此类并提供get(int index)和size()方法的实现.
要实现可修改的List,程序员必须另外覆盖set(int index,Object element)方法(否则抛出UnsupportedOperationException.如果List是可变大小的,程序员必须另外覆盖add(int index,Object element)和remove(int index)方法.
程序员通常应该根据Collection接口规范中的建议提供void(无参数)和Collection构造函数.
与其他抽象Collection实现不同,程序员不必提供Iterator实现; iterator和listIterator是由这个类实现的,最常见的是"随机访问"方法:get(int index),set(int index,Object element),set(int index,Object element),add(int index,Object element) )并删除(int index).
此类中每个非抽象方法的文档详细描述了它的实现.如果正在实施的Collection允许更有效的实现,则可以覆盖这些方法中的每一个.