为什么LinkedList 和arraylist 在java 中扩展了AbstractList?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18395005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:11:15  来源:igfitidea点击:

Why LinkedList and arraylist extends AbstractList in java?

javalistcollectionsarraylistlinked-list

提问by Raj

Why LinkedListand ArrayListextends AbstractListin Java?

为什么LinkedList和在Java 中ArrayList扩展?AbstractList

Abstract classes are used when we want to specify a common behaviour in implementation classes.

当我们想在实现类中指定一个共同的行为时使用抽象类。

But all the methods which are in AbstractListare overridden by ArrayListand LinkedList.

但是其中的所有方法AbstractList都被ArrayList和覆盖LinkedList

So what is the use of extending this class?

那么扩展这个类有什么用呢?

采纳答案by sanbhat

subList(int,int)method is not overriden by both ArrayListand LinkedList, and for this AbstractListprovides a common implementation

subList(int,int)方法不会被ArrayListand覆盖LinkedList,为此AbstractList提供了一个通用的实现

From Java source

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

In addition there are other methods which are not overriden like toString()and iterator()

此外,还有其他方法不会被覆盖,例如toString()iterator()

回答by Chris Jester-Young

Not all methods from AbstractListare overridden. Remember that AbstractListsubclasses AbstractCollection, which defines methods like containsAllor toStringwhich are not overridden by either ArrayListnor LinkedList.

并非所有方法AbstractList都被覆盖。请记住,AbstractList子类AbstractCollection定义了类似containsAll或 的方法,而这些方法toString不会被ArrayListnor覆盖LinkedList

回答by ug_

Usage is noted at the top of the AbstractList source file

用法在 AbstractList 源文件的顶部注明

"This class provides a skeletal implementation of the {@link List} interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array). For sequential access data (such as a linked list), {@link AbstractSequentialList} should be used in preference to this class."

“此类提供了 {@link List} 接口的骨架实现,以最大限度地减少实现由“随机访问”数据存储(例如数组)支持的此接口所需的工作。对于顺序访问数据(例如链表) ),{@link AbstractSequentialList} 应该优先于此类使用。”

So essentially it provides some methods to build around and a framework that is more robust than the List interface.

所以本质上它提供了一些构建方法和一个比 List 接口更健壮的框架。

回答by Mit Bhatt

You can get Answer from Here,,, AbstractList

你可以从这里得到答案,,, AbstractList

This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array). For sequential access data (such as a linked list), AbstractSequentialList should be used in preference to this Class. To implement an unmodifiable List, the programmer needs only to extend this class and provide implementations for the get(int index) and size() methods.

此类提供 List 接口的骨架实现,以最大限度地减少实现由“随机访问”数据存储(例如数组)支持的此接口所需的工作。对于顺序访问数据(如链表),应优先使用 AbstractSequentialList 而非此类。要实现一个不可修改的 List,程序员只需要扩展这个类并提供 get(int index) 和 size() 方法的实现。

To implement a modifiable List, the programmer must additionally override the set(int index, Object element) method (which otherwise throws an UnsupportedOperationException. If the List is variable-size the programmer must additionally override the add(int index, Object element) and remove(int index) methods.

要实现可修改的 List,程序员必须另外重写 set(int index, Object element) 方法(否则会抛出 UnsupportedOperationException。如果 List 是可变大小的,程序员必须另外重写 add(int index, Object element) 和remove(int index) 方法。

The programmer should generally provide a void (no argument) and Collection constructor, as per the recommendation in the Collection interface specification.

根据 Collection 接口规范中的建议,程序员通常应提供 void(无参数)和 Collection 构造函数。

Unlike the other abstract Collection implementations, the programmer does not have to provide an Iterator implementation; the iterator and listIterator are implemented by this class, on top the "random access" methods: get(int index), set(int index, Object element), set(int index, Object element), add(int index, Object element) and remove(int index).

与其他抽象 Collection 实现不同,程序员不必提供 Iterator 实现;迭代器和列表迭代器由这个类实现,在“随机访问”方法之上:get(int index), set(int index, Object element), set(int index, Object element), add(int index, Object element) ) 和 remove(int index)。

The documentation for each non-abstract methods in this class describes its implementation in detail. Each of these methods may be overridden if the Collection being implemented admits a more efficient implementation.

此类中每个非抽象方法的文档详细描述了其实现。如果正在实现的 Collection 允许更有效的实现,则可以覆盖这些方法中的每一个。