本文介绍了ArrayList<>对比 ArrayList<Integer>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! ArrayList 的以下两个声明有什么区别?What is the difference in the two following declarations of an ArrayList?ArrayList<Integer> nunbers = new ArrayList<Integer>();对比ArrayList<Integer> nunbers = new ArrayList<>();其中一个比另一个更受欢迎吗?Is one of them preferred over the other?推荐答案第二个有它的类型参数 inferred,这是Java 7中的一个新东西.被称为钻石".The second one has its type parameter inferred, which is a new thing in Java 7. <> is called "the diamond".还要注意类型推断本身在 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 (<>).我会说第二个可能是首选,只要您可以确保代码只需要在 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<>对比 ArrayList<Integer>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 11:30