Why LinkedList and arraylist extends AbstractList in java?

后端 未结 4 1358
一个人的身影
一个人的身影 2021-02-13 18:41

Why LinkedList and ArrayList extends AbstractList in Java?

Abstract classes are used when we want to specify a common beh

相关标签:
4条回答
  • 2021-02-13 18:55

    subList(int,int) method is not overriden by both ArrayList and LinkedList, and for this AbstractList provides a common implementation

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

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

    0 讨论(0)
  • 2021-02-13 19:02

    Not all methods from AbstractList are overridden. Remember that AbstractList subclasses AbstractCollection, which defines methods like containsAll or toString which are not overridden by either ArrayList nor LinkedList.

    0 讨论(0)
  • 2021-02-13 19:11

    Usage is noted at the top of the AbstractList source file

    "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."

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

    0 讨论(0)
  • 2021-02-13 19:13

    You can get Answer from Here,,, 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.

    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.

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

    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).

    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.

    0 讨论(0)
提交回复
热议问题