ArrayList的<> VS的ArrayList<整数GT; [英] ArrayList<> vs ArrayList<Integer>

查看:139
本文介绍了ArrayList的<> VS的ArrayList<整数GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么在一个ArrayList的以下两个声明的区别?

What is the difference in the two following declarations of an ArrayList?

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

VS

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

就是其中之一pferred比其他$ P $?

Is one of them preferred over the other?

推荐答案

第二个拥有类型参数的 inferred,这是在Java 7中的新事物&LT;&GT; 被称为的钻石

The second one has its type parameter inferred, which is a new thing in Java 7. <> is called "the diamond".

另外请注意,<一href=\"http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#What%20is%20type%20argument%20inference%20in%20general?\">type推论本身并不是新的Java中,但推断它被实例化泛型类的能力是新的。

Also note that type inference itself is not new in Java, but the ability to infer it for the generic class being instantiated is new.

这版本的Java SE 7之前,编译器能够推断出通用的构造,类似于通用方法的实际类型参数。然而,在Java SE 7的编译器和后可以推断出泛型类的实际类型参数被实例化,如果你使用的金刚石(小于>)。

Compilers from releases prior to Java SE 7 are able to infer the actual type parameters of generic constructors, similar to generic methods. However, compilers in Java SE 7 and later can infer the actual type parameters of the generic class being instantiated if you use the diamond (<>).

我想说的第二个可能是preferred只要你能确保code只需要在Java 7的运行,因为它是更清晰,不仅减少了冗余信息。

I'd say the second one is probably preferred as long as you can make sure the code only needs to run on Java 7, since it is clearer, and only reduces redundant information.

这篇关于ArrayList的&LT;&GT; VS的ArrayList&LT;整数GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆