List<T> 和 List<T> 之间的区别和 LinkedList<T> [英] Difference between List&lt;T&gt; and LinkedList&lt;T&gt;

查看:18
本文介绍了List<T> 和 List<T> 之间的区别和 LinkedList<T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在需要列表时使用 List.我现在注意到有一个 LinkedList.

We use List whenever we need a list. I notice now that there is a LinkedList.

我想知道这两个之间有什么区别,以及何时应该使用一个而不是另一个.

I was wondering what was the difference between these 2, and when you should use one over the other.

推荐答案

好吧,List 基本上由一个数组支持,该数组通常比当前项目的数量大.元素被放入一个数组中,当旧数组空间不足时会创建一个新数组.这对于按索引访问速度很快,但在删除或插入列表中或开头的元素时很慢.在列表末尾添加/删除条目相当便宜.

Well, List<T> is basically backed by an array which is usually bigger than the current number of items. The elements are put in an array, and a new array is created when the old one runs out of space. This is fast for access by index, but slow at removing or inserting elements within the list or at the start. Adding/removing entries at the end of the list is reasonably cheap.

LinkedList 是一个双向链表——每个节点都知道它的前一个条目和它的下一个条目.这对于在特定节点(或头部/尾部)之后/之前插入速度很快,但在按索引访问时速度较慢.

LinkedList<T> is a doubly-linked list - each node knows its previous entry and its next one. This is fast for inserting after/before a particular node (or the head/tail), but slow at access by index.

LinkedList通常List 占用更多的内存,因为它需要空间用于所有那些下一个/上一个引用 -并且数据可能具有较少的引用局部性,因为每个节点都是一个单独的对象.另一方面,List 可以有一个比其当前需要大得多的后备数组.

LinkedList<T> will usually take more memory than List<T> because it needs space for all those next/previous references - and the data will probably have less locality of reference, as each node is a separate object. On the other hand, a List<T> can have a backing array which is much larger than its current needs.

这篇关于List<T> 和 List<T> 之间的区别和 LinkedList<T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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