English 中文(简体)
名单或ArrayList的声明在 Java有何差别?
原标题:What are the List or ArrayList declaration differences in Java?

I am new to Java. I want to know the difference between:

List< String > list = new ArrayList<>();

以及

ArrayList< String > list = new ArrayList<String>();

以及

ArrayList< String > list = new ArrayList<>();

增 编

最佳回答

第一个部分自贾瓦7起就有效,相当于

List<String> list = new ArrayList<String>();

它只是 less。

第三种类型,等于

ArrayList<String> list = new ArrayList<String>();

因此,严格地相当于第二种情况。

出于对以下问题的答复中提到的原因,你应选择第一个问题:。 清单与参考类别:

问题回答

三个部分大致相同:

List<String> list = new ArrayList<>();

在上述各项内容中,您重新宣布了一个可实施<代码>List界面的变量,该接口将包含St内容,并随具体类别即时使用。 此外,你还使用 Java7新钻石辛奈,你的儿子不必再写<条码>,在<条码>与代号之间,

ArrayList<String> list = new ArrayList<String>();

以上所述,您重新宣布了具体类别<代码>ArrayList的变数,其中将包含<条码> 插入/编码>要素,并以具体类别<代码>ArrayList使用“传统”合成物,规定在<代码>和>之间指定的类型;>。

ArrayList<String> list = new ArrayList<>();

In the above, you re declaring a variable of the concrete class ArrayList which will contain String elements, and instantiate it with the concrete class ArrayList. Also, you re using Java 7 s new diamond syntax, son you don t have to write again String between the <>.

了解钻石合成物(<>)将只在 Java7及以上开展工作,而以前版本的 Java,你则重新使用传统合成物(<the-type>)。

The last two forms are completely equivalent; the first form is a bit different since you re specifying that the list variable is of type List and not of type ArrayList - and that s the preferred form, since good object-oriented practices dictate that you should program to an interface , not an implementation .

这里有两个要点:

1. 导言 新的Java 7钻石经营者允许你在不具体说明双方的类型参数的情况下,立即进行一个通用类别。 因此,这两者相当:

ArrayList< String > list = new ArrayList<String>();
ArrayList< String > list = new ArrayList<>();

2. 结 论 更重要的一点是头两个即时之间的区别。 第二点是明确的:

ArrayList< String > list = new ArrayList<String>();

第一部分:

List< String > list = new ArrayList<>();

you are using the fact that ArrayList is a subtype of List and therefore the assignment is valid. However, on the new list object you only have the subset of methods that are included in List (because your object is declared as List after all), but with the implementations present in ArrayList. This is called polymorphism in object-oriented programming and allows you to use a subtype of a class instead of the parent, where the parent is expected, in order to provide various different functionalities.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签