本文介绍了为什么LinkedList 和arraylist 在java 中扩展了AbstractList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么LinkedListArrayListJava 中扩展了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 中的所有方法都被 ArrayListLinkedList 覆盖.

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) 方法不会被 ArrayListLinkedList,并为此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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:25