java 菱形运算符 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30048172/
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-11-02 16:21:33  来源:igfitidea点击:

Diamond operator ArrayList

javaarraylist

提问by tamaramaria

What is the difference between

之间有什么区别

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

and

ArrayList<Integer> list = new ArrayList();

Are the diamond operator when I create a new ArrayList necessary?

创建新 ArrayList 时是否需要菱形运算符?

回答by AlexR

The first statement is clear. The second generates compilation warning.

第一个说法是明确的。第二个生成编译警告。

Diamond iperator was introduced to java 1.7. Starting from java 1.5 you had to write

Diamond iperator 被引入到 java 1.7。从 Java 1.5 开始,您必须编写

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

i.e. declare the generic type both on the left and right sides of the assignment. Since generic types may be more complex than just Integerit was annoying to copy exactly the same definition twice, so they added this cool feature to compiler: you just have to say: "this class is generic, use the generic type from the left side of the assignment" by using <>marker.

即在赋值的左侧和右侧声明泛型类型。由于泛型类型可能更复杂,而不仅仅是Integer将完全相同的定义复制两次很烦人,所以他们向编译器添加了这个很酷的功能:你只需要说:“这个类是泛型的,使用左侧的泛型类型分配”通过使用<>标记。

BTW the last comment. Please avoid using concrete classes in the left side of assignments and method definitions. The following is much better:

顺便说一句,最后一条评论。请避免在赋值和方法定义的左侧使用具体类。以下要好得多:

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

or even

甚至

Collection<Integer> list = new ArrayList<>();

or often even

甚至经常

Iterable<Integer> list = new ArrayList<>();

回答by neon64

A rather advanced topic for Java is called Genericsand that is the explanation for all the <> symbols.

Java 的一个相当高级的主题称为泛型,这是对所有 <> 符号的解释。

In essence, you should always write

本质上,你应该总是写

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

Because it is shorthand for

因为它是简写

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

And you need both type arguments (the bit) to match otherwise the compiler will give you a warning.

并且您需要两个类型参数(位)匹配,否则编译器会给您一个警告。

Writing:

写作:

ArrayList<Integer> list = new ArrayList();

Creates an ArrayList that can hold any type, and when you try to convert it into an ArrayList<Integer>the compiler will throw a warning.

创建一个可以容纳任何类型的 ArrayList,当您尝试将其转换为 ArrayList 时ArrayList<Integer>,编译器将发出警告。

EDIT:

编辑:

Here is a in-depth guide on what generics are for and how they work. I'm not sure if you're at the level to understand them fully yet, but it's worth a try.

这是关于泛型的用途及其工作方式的深入指南。我不确定您是否处于完全理解它们的水平,但值得一试。

https://docs.oracle.com/javase/tutorial/java/generics/

https://docs.oracle.com/javase/tutorial/java/generics/

回答by Aakash

First is the parameterized generic object of ArrayList, referenced by a parameterized generic reference.

首先是 的参数化泛型对象ArrayList,由参数化泛型引用引用。

Second is non-parameterized non-generic object of ArrayList, referenced by a parameterized generic reference.

第二个是 的非参数化非泛型对象ArrayList,由参数化泛型引用引用。

No, although the diamond operators are not necessary when you create an ArrayList, it is highly recommended to avoid ClassCastExceptionor some other RuntimeExceptionlater on.

不,虽然创建 时不需要菱形运算符ArrayList,但强烈建议稍后避免ClassCastException或其他一些RuntimeException