List 和 ArrayList 有什么区别? [英] What is the difference between List and ArrayList?

查看:24
本文介绍了List 和 ArrayList 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在办公室的android项目中一直在使用ArrayList,对List和ArrayList有点困惑,两者有什么区别,应该用什么?

I've been using ArrayList recently in my android project at the office and I'm a bit confused between List and ArrayList, what is the difference of the two and what should I use?

我也看到了它的一些实现.

Also I saw some implementations of it like.

List<SomeObject> myList = new ArrayList<SomeObject>();

ArrayList<SomeObject> myList = new ArrayList<SomeObject>();

这两个实例有什么区别?

What is the difference of those two instances?

推荐答案

您的两个示例中的列表实现之间没有区别.但是,您可以在代码中进一步使用变量 myList 的方式有所不同.

There's no difference between list implementations in both of your examples. There's however a difference in a way you can further use variable myList in your code.

当您将列表定义为:

List myList = new ArrayList();

您只能调用在 List 接口中定义的方法和引用成员.如果您将其定义为:

you can only call methods and reference members that are defined in the List interface. If you define it as:

ArrayList myList = new ArrayList();

除了定义从 List 继承的成员之外,您还可以调用特定于 ArrayList 的方法并使用特定于 ArrayList 的成员.

you'll be able to invoke ArrayList-specific methods and use ArrayList-specific members in addition to those whose definitions are inherited from List.

然而,当你在第一个例子中调用一个 List 接口的方法时,它是在 ArrayList 中实现的,来自 ArrayList 的方法将被调用(因为 List 接口没有实现任何方法).

Nevertheless, when you call a method of a List interface in the first example, which was implemented in ArrayList, the method from ArrayList will be called (because the List interface doesn't implement any methods).

这就是所谓的多态性.你可以阅读它.

That's called polymorphism. You can read up on it.

这篇关于List 和 ArrayList 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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