问题描述
可能的重复:
什么时候在c#中使用ArrayList over array[]?
从内存或处理器成本的角度来看,数组和 arrayList 对象之间似乎存在显着差异?
From the perspective of memory or processor costs, does there appear to be a significant difference between an array and an arrayList object?
推荐答案
数组是一种底层数据结构,本质上映射到内存中的一个区域.ArrayList
是一个可变长度列表,实现为一个 object
数组,随着列表的增长而重新分配.
An array is a low-level data structure that essentially maps to a region in memory. An ArrayList
is a variable length list implemented as an array of object
that is re-allocated as the list grows.
ArrayList
因此有一些与管理内部数组大小相关的开销,以及更多与访问列表时将对象转换为正确类型相关的开销.
ArrayList
therefore has some overhead related to managing the size of the internal array, and more overhead related to casting objects to the correct type when you access the list.
此外,将所有内容存储为 object
意味着值类型在写入时被装箱并在读取时被取消装箱,这对性能极为不利.使用 List
,一个类似但强类型的可变大小列表避免了这个问题.
Also, storing everything as object
means that value types get boxed on write and unboxed on read, which is extremely detrimental to performance. Using List<T>
, a similar but strongly-typed variable size list avoids this issue.
事实上,从 .NET 2.0 开始,ArrayList
实际上已被弃用,取而代之的是 List
.
In fact, ArrayList
is practically deprecated in favor of List<T>
since .NET 2.0.
这篇关于数组与数组列表的显着差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!