间表和ArrayList的区别是什么?区别、ArrayList

2023-09-11 20:34:22 作者:想你想到手长茧

我一直在使用的ArrayList最近在我的办公室的Andr​​oid项目,我有点糊涂了列表和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中在code。

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 belong to List class. If you define it as:

ArrayList myList = new ArrayList();

您就可以调用ArrayList的具体方法和使用ArrayList的特定成员除了那些从名单继承。

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

然而,当你调用在第一个例子中的List类,它是在ArrayList中覆盖的方法,然后从ArrayList的方法将被称为没有一个在列表中。

Nevertheless, when you call a method of a List class in the first example, which was overridden in ArrayList, then method from ArrayList will be called not the one in the List.

这就是所谓的多态。你可以在上面写了起来。

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