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<>();
增 编
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
内容,并随具体类别
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.
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...