Java 中的 List 或 ArrayList 声明有何不同? [英] What are the List or ArrayList declaration differences in Java?

查看:31
本文介绍了Java 中的 List 或 ArrayList 声明有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手.我想知道两者之间的区别:

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<>();

谢谢

推荐答案

第一个只从 Java 7 开始有效,相当于

The first one is only valid since Java 7, and is the equivalent of

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

只是不那么冗长.

第三个相同,相当于

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

因此严格等同于第二个.

and thus strictly equivalent to the second one.

您应该更喜欢第一个,原因在以下问题的答案中提到:List 与 ArrayList 作为引用类型?

You should prefer the first one, for the reasons mentioned in the answers to the following question: List versus ArrayList as reference type?

这篇关于Java 中的 List 或 ArrayList 声明有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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