LinkedList、队列与列表的区别 [英] Difference in LinkedList, queue vs list

查看:20
本文介绍了LinkedList、队列与列表的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建这两个对象有什么区别

What is the difference when creating these two objects

Queue<String> test = new LinkedList<String>();

List<String> test2 = new LinkedList<String>();

testtest2 之间的实际区别是什么?他们都是 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 对象来保存字符串列表,然后将其分配给一个变量.区别在于变量的类型.

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赋值给Queue类型的变量,你只能访问LinkedList中的方法在 Queue 接口中可用,其中包括对入队和出队元素的支持.如果您需要编写使用队列进行各种操作的程序,并希望使用链表实现该队列,这将非常有用.

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赋值给List类型的变量,你只能访问LinkedList中的方法在 List 接口中可用,这是维护元素序列的正常操作.这将很有用,例如,如果您需要处理可以随处增长和缩小的元素列表.

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屋!

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