LinkedList,队列与列表的区别 [英] Difference in LinkedList, queue vs list
问题描述
创建这两个对象有什么区别
What is the difference when creating these two objects
Queue<String> test = new LinkedList<String>();
和
List<String> test2 = new LinkedList<String>();
test
之间的实际差异是什么? TEST2
?它们都是 LinkedList
?是否存在使用其中一个的性能差异或原因?
What are the actual differences between test
and test2
? Are both of them LinkedList
? Are there performance differences or reasons to use one over the other?
推荐答案
您编写的两个语句构成一个 LinkedList< String>
用于保存字符串列表的对象,然后将其分配给变量。区别在于变量的类型。
The two statements you've written each construct a LinkedList<String>
object to hold a list of strings, then assign it to a variable. The difference is in the type of the variable.
通过将 LinkedList< String>
分配给类型变量 Queue< String>
,您只能访问 LinkedList
中的方法> Queue< String> interface,包括对元素入队和出队的支持。如果您需要编写一个使用队列进行各种操作并希望使用链表实现该队列的程序,这将非常有用。
By assigning the LinkedList<String>
to a variable of type Queue<String>
, you can only access the methods in the LinkedList
that are available in the Queue<String>
interface, which includes support for enqueuing and dequeuing elements. This would be useful if you needed to write a program that used a queue for various operations and wanted to implement that queue by using a linked list.
通过分配 LinkedList< String>
到类型 List< String>
的变量,你只能访问<$ c $中的方法c> LinkedList 在 List< String>
界面中可用,它们是维护一系列元素的常规操作。这将是有用的,例如,如果您需要处理可在任何地方增长和缩小的元素列表。
By assigning the LinkedList<String>
to a variable of type List<String>
, you can only access the methods in the LinkedList
that are available in the List<String>
interface, which are normal operations for maintaining a sequence of elements. This would be useful, for example, if you needed to process a list of elements that could grow and shrink anywhere.
简而言之,这两行创建相同的对象但打算以不同的方式使用它们。一个说它需要一个由链表支持的队列,而另一个说它需要一个由链表支持的一般元素序列。
In short, the two lines create the same object but intend to use them in different ways. One says that it needs a queue backed by a linked list, while the other says that it needs a general sequence of elements backed by a linked list.
希望这有帮助!
这篇关于LinkedList,队列与列表的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!