Java List<String> list = new ArrayList<String>() 和 ArrayList<String> list = new ArrayList<String>() 有什么区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23991119/
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
What is difference between List<String> list = new ArrayList<String>() and ArrayList<String> list = new ArrayList<String>()?
提问by user3300570
Hi I have not a clear idea between,
嗨,我没有明确的想法,
List<String> list = new ArrayList<String>();
and
和
ArrayList<String> list = new ArrayList<String>();
回答by rhbvkleef
ArrayList creates an object of type ArrayList and List creates an object of type List, ArrayLists underlying interface. Search the javadocs if you dont know what interfaces are.
ArrayList 创建一个 ArrayList 类型的对象,List 创建一个 List 类型的对象,ArrayLists 底层接口。如果您不知道什么是接口,请搜索 javadoc。
回答by konsolebox
List<String>
is a superclass (or could be a valid interface) of ArrayList<String>
. Assigning an instance of ArrayList<String>
to a List<String>
variable is allowed. It's somehow a form of dynamic casting. When accessing List<String> list
, only methods accessible with List<String>
could be used; and those from ArrayList<String>
would be hidden despite the object being an instance of ArrayList<String>
.
List<String>
是 的超类(或可能是有效的接口)ArrayList<String>
。允许将 的实例分配ArrayList<String>
给List<String>
变量。不知何故,它是一种动态转换的形式。访问时List<String> list
,只能使用可访问的方法List<String>
;而那些从ArrayList<String>
会,尽管对象是的实例被隐藏ArrayList<String>
。
回答by Rahul Bobhate
In both the cases, you create object of ArrayList
. But List<String> list = new ArrayList<String>();
will refer the object by using a reference of List<String>
whereas ArrayList<String> list = new ArrayList<String>();
will refer the object by using a reference variable of type ArrayList<String>
.
在这两种情况下,您都创建ArrayList
. 但是List<String> list = new ArrayList<String>();
将通过使用 的引用来引用对象,List<String>
而ArrayList<String> list = new ArrayList<String>();
将通过使用类型的引用变量来引用对象ArrayList<String>
。
For example, you can call a method named ensureCapacity
by using a reference of ArrayList<String>
, but you can't do that using a reference of List<String>
.
例如,您可以ensureCapacity
使用 的引用来调用命名的方法ArrayList<String>
,但不能使用 的引用来执行此操作List<String>
。
The following will compile:
以下将编译:
ArrayList<String> list = new ArrayList<String>();
list.ensureCapacity(10); // This will work
The following won't compile:
以下不会编译:
List<String> list = new ArrayList<String>();
list.ensureCapacity(10); // This will not compile
回答by Oleg Sklyar
With respect to the instance in memory that you obtain there is no difference. However, it is considered a good practice to Program to Interfaces (see e.g. What does it mean to "program to an interface"?). Many Java APIs are defined in terms of interfaces, i.e. the functionality will work independently on which implementation of a particular interface you use. This aids code clarity and quality and reduces the probability of bugs which arise from relying on some particular properties of an implementation class (unless these properties are really important).
对于您获得的内存中的实例,没有区别。然而,编程到接口被认为是一个很好的做法(参见例如“编程到接口”是什么意思?)。许多 Java API 是根据接口定义的,即功能将独立于您使用的特定接口的实现。这有助于代码的清晰度和质量,并降低因依赖实现类的某些特定属性而产生错误的可能性(除非这些属性非常重要)。
回答by Husman
List<String> list = new ArrayList<String>();
Creates a List of type string, and the list can be potentially typecast into any other type of list. This is called decoupling. You are decoupling your code from a specific implementation of the interface. The advantages it provides, is when writing large amounts of code, you can switch between types of lists to suit your preferences (speed, memory etc), as all of your code, can treat your list as just type List. You can also pass a List as parameters and returns List from functions. And later on if you are not happy with the ArrayList, you just change that one line of code
创建一个字符串类型的列表,并且该列表可以潜在地被类型转换为任何其他类型的列表。这称为解耦。您正在将代码与接口的特定实现分离。它提供的优点是,在编写大量代码时,您可以在列表类型之间切换以满足您的偏好(速度、内存等),因为您的所有代码都可以将您的列表视为类型 List。您还可以将 List 作为参数传递并从函数返回 List。稍后如果您对 ArrayList 不满意,您只需更改那一行代码
List<String> list = new ArrayList<String>(); // old code
List<String> list = new LinkedList<String>(); // new code
// The rest of the code doesnt need changing
...
list = getList();
...
public List<String> getList() {
List<String> temporaryList;
...
return temporaryList;
}
public void changeList(List<string> localListVariable) {}
And your program will behave as expected.
您的程序将按预期运行。
On the other hand,
另一方面,
ArrayList<String> list = new ArrayList<String>();
Creates an ArrayList of String types and it cannot be used as any other kind of List (Vector,LinkedList etc). Therefore it is bound by the methods available to an ArrayList.
If you now want to change the type of list used, you will have to change all function parameters and return types and so on throughout your program (wherever you have had to create an ArrayList<String>
to work with your variable).
创建一个 String 类型的 ArrayList,它不能用作任何其他类型的 List(Vector、LinkedList 等)。因此,它受 ArrayList 可用方法的约束。如果您现在想要更改所使用的列表类型,则必须在整个程序中更改所有函数参数和返回类型等(无论您必须在何处创建 anArrayList<String>
来处理您的变量)。