为什么LinkedList和arraylist在java中扩展AbstractList? [英] Why LinkedList and arraylist extends AbstractList in java?

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

问题描述

为什么 LinkedList ArrayList 在<中扩展 AbstractList em> Java ?

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?

推荐答案

ArrayList LinkedList ,并为此 AbstractList 提供了一个通用的实现

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

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

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

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

这篇关于为什么LinkedList和arraylist在java中扩展AbstractList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆