List or ArrayList?(lots of items)

Beh*_*ooz 2 .net list arraylist

which one is more memory efficient?
which one works faster with 1000000 items?
is there anything better?

Cha*_*lie 13

A List<T> is generally preferable to using an ArrayList because it is a type safe collection. This means that you get build time type safety. It is also more memory efficient for value types because entries in an ArrayList will be boxed because its a list of type object:

例如:向a添加一个整数List<int>会将数据int[]作为基础数据结构放在堆上.向an添加整数ArrayList会将数据放在堆上,但由于底层数据结构是a object[],因此必须将数据装箱,这意味着指针也必须存储在堆上,这需要分配更多内存.

a ArrayList和a List<T> : class(引用类型列表)的内存分配完全相同.


Rub*_*ias 5

List<T>ArrayList以下更好:

特定类型(除Object之外)的Array具有比ArrayList更好的性能,因为ArrayList的元素是Object类型,因此,在存储或检索值类型时通常会发生装箱和取消装箱.但是,如果不需要重新分配,List可以具有与相同类型的数组类似的性能; 也就是说,如果初始容量是列表最大大小的良好近似值.

ArrayList和List Collection Types