简体   繁体   English

创建一个LinkedList

[英]Creating a LinkedList

If I create a LinkedList like this: 如果我这样创建一个LinkedList

LinkedList<Integer> Mistery = new LinkedList<>();

It will be possible to implement this: Mistery.getFirst(); 可以实现这一点: Mistery.getFirst();

If, on the other hand, I create a LinkedList like this: 另一方面,如果我创建这样的LinkedList:

List<Integer> Mistery = new LinkedList<>();

It won't be possible to implement the getFirst() method on the Mistery list. Mistery列表上将无法实现getFirst()方法。

Why? 为什么?

因为您将Mistery声明为超级类型: List (尽管您选择了LinkedList作为实现),所以在List接口中没有定义getFirst()方法。

List<Integer> Mistery = new LinkedList<>();

By declaring the variable this way, you're telling the compiler: I want to use a List. 通过以这种方式声明变量,您就是在告诉编译器:我要使用List。 The implementation of the List I want is LinkedList, but the rest of the code shouldn't know about that. 我想要的列表的实现是LinkedList,但是其余代码不应该知道这一点。 All it needs to know is that it's a List. 它所需要知道的就是它是一个列表。 I might change my mind later and use an ArrayList, and the rest of the code would still compile, because ArrayList is also a List. 稍后我可能会改变主意并使用ArrayList,并且其余代码仍将编译,因为ArrayList也是一个List。 I could also assign another object to the same variable, and this object might be of type ArrayList, or CopyOnWriteArrayList, or any other type of List. 我还可以将另一个对象分配给相同的变量,并且该对象的类型可以是ArrayList或CopyOnWriteArrayList或任何其他类型的List。

Since List doesn't have a getFirst() method, you can't use it: the variable is of type List, not LinkedList. 由于List没有getFirst()方法,因此您无法使用它:变量的类型为List,而不是LinkedList。 At runtime, sure, the object is of type LinkedList. 在运行时,请确保该对象的类型为LinkedList。 But the compiler only knows the type of the variable. 但是编译器只知道变量的类型。 It doesn't know its concrete type at runtime. 它在运行时不知道其具体类型。

Side note: Java variables should start with a lowercase letter, to respect the standard conventions. 旁注:Java变量应以小写字母开头,以遵守标准约定。

Something to think about is also why you would want to use List. 还有一些要考虑的原因也是您为什么要使用List的原因。 (Answers already covering why List doesn't have getFirst() method). (答案已经涵盖了为什么List没有getFirst()方法)。

Say you have a method that prints content of a list: 假设您有一种打印列表内容的方法:

public printStuff(List list){
   for(Object element: list){
       System.out.println(element);}

By declaring your Mistery list a List it means you could then (through polymorphism ), use a single method for a range of lists ie 通过将您的Mistery列表声明为List,这意味着您可以(通过polymorphism )对列表范围使用单一方法,即

List<Integer> Mistery = new LinkedList<>();
List<Integer> Mistery2 = new ArrayList<>();
List<String> Mistery3 = new ArrayList<>();
//etc

Then you can do this: 然后,您可以执行以下操作:

printStuff(Mistery);
printStuff(Mistery2);
printStuff(Mistery3); //can even print out a List which contains Strings

If the List was declared like this LinkedList<Integer> Mistery = new LinkedList<>(); 如果列表是这样声明的LinkedList<Integer> Mistery = new LinkedList<>(); you could not use the method printStuff . 不能使用方法printStuff You should also read up on casting . 您还应该阅读有关铸造的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM