java - List与ArrayList
PHP中文网
PHP中文网 2017-04-18 09:42:00
0
5
445

List<Sting> list =new ArrayList<>();
和List<Sting> list =new ArrayList<String>();
两者有何区别?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
PHPzhong

No difference. . The data type is specified when List<String>.

JDK 1.7 features added support for type inference.

Enhanced type inference for generic instance creation (diamond)

  类型推断是一个特殊的烦恼,下面的代码: 
     Map<String, List<String>> anagrams = new HashMap<String, List<String>>(); 

  通过类型推断后变成: 
     Map<String, List<String>> anagrams = new HashMap<>(); 
  这个<>被叫做diamond(钻石)运算符,这个运算符从引用的声明中推断类型。 
  
洪涛

There is no difference. Starting from jdk1.7, you can omit the String in the angle brackets on the right.
(The right-hand side is inferred by the generic within the declaration, so no need.)

迷茫

If you express it

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

There is no difference between the two. The above ArrayList will also be automatically converted to the generic type of String. The data type that the list can access can only be the String type. And if your first line of code is changed to

List list = new ArrayList();

This makes a big difference. The data type accessed by list is not fixed. You can access String, Integer or even Object types.

阿神

The first one must be written after jdk1.7, otherwise there will be a compilation error. The latter one must be written before 1.7. Of course, it can also be written like this after 1.7

左手右手慢动作

After jdk1.7, there is no difference

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!