问题描述
为什么LinkedList
和ArrayList
在Java 中扩展了AbstractList
?
Why LinkedList
and ArrayList
extends AbstractList
in Java?
当我们想在实现类中指定一个共同的行为时使用抽象类.
Abstract classes are used when we want to specify a common behaviour in implementation classes.
但是 AbstractList
中的所有方法都被 ArrayList
和 LinkedList
覆盖.
But all the methods which are in AbstractList
are overridden by ArrayList
and LinkedList
.
那么扩展这个类有什么用?
So what is the use of extending this class?
推荐答案
subList(int,int)
方法不会被 ArrayList
和 LinkedList,并为此
AbstractList
提供了一个通用的实现
subList(int,int)
method is not overriden by both ArrayList
and LinkedList
, and for this AbstractList
provides a common implementation
来自 Java 源代码
From Java source
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<E>(this, fromIndex, toIndex) :
new SubList<E>(this, fromIndex, toIndex));
}
此外还有其他没有被覆盖的方法,如 toString()
和 iterator()
In addition there are other methods which are not overriden like toString()
and iterator()
这篇关于为什么LinkedList 和arraylist 在java 中扩展了AbstractList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!