ArrayList到底存储什么?
我知道我可以将任何类型的对象添加到 ArrayList 实例中。如果我做对了,那么引用类型将被转换为对象(值类型被装箱)。另外,ArrayList 是否真的存储引用类型对象的对象列表?
I know I can add object of any type to an ArrayList
instance. If I get it right, then reference types are casted to objects (value types are boxed). Also, does an ArrayList
actually store lists of objects of reference type object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
内部
ArrayList
类使用固定大小的数组object[]
(对象数组)进行存储。添加元素时,这些元素会自动复制到数组中各自的索引。当达到最大大小时,将创建一个具有更大大小的新数组,并重新复制元素。所以它只是静态对象数组的一个方便的包装器。Internally the
ArrayList
class uses a fixed size arrayobject[]
(object array) for storage. When you add elements those elements are automatically copied to their respective indexes in the array. When the max size is reached a new array is created with a larger size and the elements are recopied. So it's just a convenience wrapper around a static object array.ArrayList 不存储对象,而仅存储对这些对象的引用。
An
ArrayList
does not store objects, but merely the references to those objects.ArrayList
本质上是一个object[]
的包装器,具有跟踪数组空间并根据需要将其增长(加倍)的功能。请注意,通常List
是首选,但要回答这个问题:是的,它只存储对object
的引用,这些对象很可能是装箱的值类型。List
几乎相同,但围绕T[]
,这意味着可以在不装箱的情况下存储值类型。引用类型仍然存储为引用。您还可以获得更多的类型安全性;即你不能添加错误的东西,也不能不正确地投射检索到的项目。An
ArrayList
is essentially a wrapper around anobject[]
, with functionality to track space in the array and grow it (double it) as necessary. Note that usuallyList<T>
is preferred, but to answer the question: yes, it just stores the references to theobject
s, which may well be boxed value-types.A
List<T>
is pretty much the same, but around aT[]
, which means value types can be stored without boxing. Reference-types are still stored as references. You also get more type safety; i.e. you can't add the wrong thing nor cast a retrieved item improperly.ArrayList
与List
它可以存储从
object
派生的任何内容,这些都是引用和值类型。所以你可以用它来存储对象列表。ArrayList
is similar toList<object>
and was created before .NET had generics.It can store anything that derives from
object
, which is all reference and value types. So you could use it to store a lists of objects.ArrayList
的内部存储是一个对象数组 (object[]
)。当在
ArrayList
中存储引用类型时,引用只是转换为object
并存储在数组中。引用类型实例本身包含有关其类型的信息,因此当您从 ArrayList 获取它时,可以将其转换回实际类型。值类型被装箱在对象内,对该对象的引用存储在数组中。该对象包含有关该值的类型的信息,以便当您从 ArrayList 获取该值时可以正确拆箱。
The internal storage for an
ArrayList
is an object array (object[]
).When storing reference types in the
ArrayList
, the reference is just cast toobject
and stored in the array. The reference type instance itself contains information about it's type, so it's possible to cast it back to the actual type when you get it from theArrayList
.Value types are boxed inside an object, and the reference to that object is stored in the array. The object contains information about what type the value is, so that it can be unboxed correctly when you get it from the
ArrayList
.它们存储任何
对象
。这意味着只要它们被实例化为对象,就可以用字符串、整数、类来填充它们。
They store any
object
.Meaning it's possible to fill them with strings, integers, classes as long as they have been instantiated as an object.