java OOP:ArrayList al = new ArrayList() 和 List al = new ArrayList() 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4592558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:07:12  来源:igfitidea点击:

OOP: Difference between ArrayList al = new ArrayList() and List al = new ArrayList()?

javacollections

提问by Hari kanna

Possible Duplicate:
List versus ArrayList

可能的重复:
List 与 ArrayList

Difference between

之间的区别

ArrayList al = new ArrayList() 

and

List al = new ArrayList() ?

采纳答案by Andreas Dolk

None, from a creation perspective. Both create an instance of ArrayList.

没有,从创作的角度来看。两者都创建了一个ArrayList.

The difference is that, in you second example, alallows access to all methods implemented on the Listinterface while, in the first example, alallows access to all (accessible) methods and fields of the ArrayListclass.

不同之处在于,在第二个示例中,al允许访问List接口上实现的所有方法,而在第一个示例中,al允许访问类的所有(可访问)方法和字段ArrayList

A practical rule of thumb: use the second pattern. If you need some extra goodies from the ArrayListimplementation, then you can always cast:

一个实用的经验法则:使用第二种模式。如果您需要从ArrayList实现中获得一些额外的好处,那么您可以随时转换:

 List list = new ArrayList();
 // do some adds/removes/... on the list
 ((ArrayList) list).trimToSize();

回答by T.J. Crowder

Paraphrasing my answerto this very similar questionabout Mapvs. HashMap:

解释这个关于Mapvs. 的非常相似的问题的回答HashMap

There is no difference between the objects. There is a difference in the interfaceyou have to the object. In the first case, the interface is ArrayList, whereas in the second it's List. The underlying object, though, is the same.

对象之间没有区别。对象的接口有所不同。在第一种情况下,接口是ArrayList,而在第二种情况下是List。但是,底层对象是相同的。

The advantage to using Listis that you can change the underlying object to be a different kind of list without breaking your contract with any code that's using it. If you declare it as ArrayList, you have to change your contract if you want to change the underlying implementation.

使用的好处List是您可以将底层对象更改为不同类型的列表,而不会破坏与使用它的任何代码的合同。如果将其声明为ArrayList,则如果要更改底层实现,则必须更改合同。

回答by Adeel Ansari

Its called programming to interface. Suppose, you need to return this listfrom your method. So the calling code can have that into a Listvariable.

它被称为编程接口。假设,您需要list从您的方法中返回 this 。所以调用代码可以将它放入一个List变量中。

public ArrayList getList() {
   ArrayList list = new ArrayList();
    // do something with the list here
   return list;
}

And this,

还有这个,

public List getList() {
   List list = new ArrayList();
    // do something with the list here
   return list;
}

Now for the latter method calling code can have the returned list in Listtype variable. And you can easily decide later, for some reason, something like this,

现在对于后一种方法,调用代码可以在List类型变量中返回列表。你以后可以很容易地决定,出于某种原因,像这样的事情,

public List getList() {
   List list = new LinkedList();
    // do something with the list here
   return list;
}

No change in calling code, whereas with the former you need to change the return type, that would eventually screw up the calling code as well.

调用代码没有变化,而前者你需要改变返回类型,这最终也会搞砸调用代码。