java “List<Integer> list = new ArrayList<Integer>(); ”究竟是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43462404/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:26:20  来源:igfitidea点击:

What does "List<Integer> list = new ArrayList<Integer>(); " actually mean?

javalistarraylist

提问by Caroline Yi

I am pretty new to Java and I wanted to know what this actually means:

我对 Java 很陌生,我想知道这实际上意味着什么:

List<Integer> list = new ArrayList<Integer>(); //Example 1

To distinguish this question from others, I've read the posts about polymorphism and the difference between Example 1 and Example 2, and I understand that Example 1 allows for "programming to interface." I also understand that with Example 1, list can easily be changed to LinkedList without affecting the rest of the code.

为了将这个问题与其他问题区分开来,我阅读了有关多态性以及示例 1 和示例 2 之间的区别的帖子,我了解示例 1 允许“编程接口”。我也明白在示例 1 中,list 可以轻松更改为 LinkedList,而不会影响其余代码。

ArrayList<Integer> list = new ArrayList<Integer>(); //Example 2

But what I want to know what Example 1 actually means. Does it create a new List? Or does it create a new ArrayList? Does the resulting object have the properties of a List? Or does the resulting object have the properties of an ArrayList? Could I implement methods that an ArrayList uses on list without a compile error?

但是我想知道示例 1 的实际含义。它会创建一个新列表吗?或者它是否创建了一个新的 ArrayList?结果对象是否具有列表的属性?或者结果对象是否具有 ArrayList 的属性?我可以在没有编译错误的情况下实现 ArrayList 在列表上使用的方法吗?

This is my first time posting a question, so please let me know if I can make any improvements.

这是我第一次发布问题,所以如果我可以做任何改进,请告诉我。

回答by Erwin Bolwidt

In both your examples, only the expression newcreates instances of objects (the right hand side of the =), the left hand side only defines a variable that points to the object that you created.

在您的两个示例中,只有表达式new创建对象的实例( 的右侧=),左侧仅定义一个指向您创建的对象的变量。

In both cases, the object that you create is of type ArrayList.

在这两种情况下,您创建的对象都是 类型ArrayList

Listis an interface, not a class, and interfaces themselves cannot be instantiated. But ArrayListimplementsthe interface List, so you can assign an instance of ArrayListto a variable of type List.

List是一个接口,而不是一个类,接口本身不能被实例化。但是ArrayList实现了接口List,因此您可以将 的实例分配给ArrayList类型为 的变量List

The advantage of Example 1 is that you can later decide to create another implementation of List(like LinkedList) and all your code that was using the Listtype for variables will still work without any change. While in Example 2, if you later decide that you need a different type of List, you need to change the type of all your variables that point to the actual object.

示例 1 的优点是您以后可以决定创建List(如LinkedList)的另一个实现,并且所有使用List变量类型的代码仍然可以正常工作而无需任何更改。而在示例 2 中,如果您稍后决定需要不同类型的List,则需要更改指向实际对象的所有变量的类型。

Your questions:

你的问题:

But what I want to know what Example 1 actually means. Does it create a new List? Or does it create a new ArrayList?

但是我想知道示例 1 的实际含义。它会创建一个新列表吗?或者它是否创建了一个新的 ArrayList?

It creates a new ArrayList.

它创建了一个新的ArrayList.

Does the resulting object have the properties of a List? Or does the resulting object have the properties of an ArrayList?

结果对象是否具有列表的属性?或者结果对象是否具有 ArrayList 的属性?

The resulting object hasall the properties of an ArrayList.

生成的对象具有 的所有属性ArrayList

However, through the variable listwhich as type List, you can only access the methods defined in the interfaces List.

但是,通过变量listwhich as type List,您只能访问接口中定义的方法List

But you can use type-castingto access the methods in ArrayListlike this, if you later need to (but there is little reason as ArrayList doesn't have much beyond what's in List)

但是您可以使用类型转换来访问这样的方法ArrayList,如果您以后需要的话(但没有什么理由,因为 ArrayList 没有太多超出 List 中的内容)

List<Integer> list = new ArrayList<Integer>(); //Example 1
ArrayList<Integer> arrayList = (ArrayList<Integer>) list; // type-cast

回答by Fernando Tan

As you might already know, List is an interface and ArrayList is a class. It means List is more generals whereas ArrayList is more specific.

您可能已经知道,List 是一个接口,而 ArrayList 是一个类。这意味着 List 更通用,而 ArrayList 更具体。

In example 1, you are not able to invoke the additional methods supported by ArrayList e.g. you cannot invoke ensureCapacity because it is implemented inside Arraylist class.

在示例 1 中,您无法调用 ArrayList 支持的其他方法,例如您无法调用 ensureCapacity,因为它是在 Arraylist 类中实现的。

Unlike example 2, you can invoke all functionalities that are supported by ArrayList. The benefit of using example 1 is to increase flexibility and minimize code changes in case the implementation changes, e.g. in future you decide to use LinkedList. You no need to worry about making any changes to all the lines where list variable has been used.

与示例 2 不同,您可以调用 ArrayList 支持的所有功能。使用示例 1 的好处是增加灵活性并在实现更改的情况下最小化代码更改,例如,将来您决定使用 LinkedList。您无需担心对使用列表变量的所有行进行任何更改。

回答by Jacob G.

Declaring a variable from its implemented interface is normally preferred, however depending on which interface or abstract class you use, you could lose access to some methods.

通常首选从其实现的接口声明变量,但是根据您使用的接口或抽象类,您可能无法访问某些方法。

Basically, it allows your program to become more flexible.

基本上,它可以让您的程序变得更加灵活。

Regarding your question:

关于你的问题:

Your first example declares a Listand initializes it as an ArrayList. You will only be able to call methods from the Listinterface and its parent classes/interfaces.

您的第一个示例声明了 aList并将其初始化为ArrayList. 您将只能从List接口及其父类/接口调用方法。

Your second example declares an ArrayListand initializes it as an ArrayList, meaning you now have access to call methods in the ArrayListclass, such as ArrayList#ensureCapacityor ArrayList#trimToSize.

您的第二个示例声明 anArrayList并将其初始化为 an ArrayList,这意味着您现在可以访问类中的调用方法ArrayList,例如ArrayList#ensureCapacityor ArrayList#trimToSize

回答by alayor

Suppose you have a method that receives a Listobject as parameter.

假设您有一个接收List对象作为参数的方法。

public void myMethod(List<Integer> myList) { ... }

By using the example 1 you have the flexibility of passing different implementations of the Listinterface to this method without any type of casting.

通过使用示例 1,您可以灵活地将List接口的不同实现传递给此方法,而无需进行任何类型的转换。

List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new LinkedList<Integer>();
myMethod(list1);
myMethod(list2);

回答by Fady Saad

It doesn't belong to java, It is belong to OOP:

它不属于java,它属于OOP:

Nothing actually happens to the object when you assign a reference to it to a variable of some super type or interface. You will only be able to invoke methods visible to that declared type on the variable.

当您将对该对象的引用分配给某个超类型或接口的变量时,该对象实际上不会发生任何事情。您将只能调用对该变量声明的类型可见的方法。

And remember that any class implement an interface must implement all of it is methods

请记住,任何实现接口的类都必须实现所有的方法