ArrayList到底存储什么?

发布于 2024-10-31 06:53:28 字数 99 浏览 1 评论 0原文

我知道我可以将任何类型的对象添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

淡写薰衣草的香 2024-11-07 06:53:28

内部 ArrayList 类使用固定大小的数组object[](对象数组)进行存储。添加元素时,这些元素会自动复制到数组中各自的索引。当达到最大大小时,将创建一个具有更大大小的新数组,并重新复制元素。所以它只是静态对象数组的一个方便的包装器。

Internally the ArrayList class uses a fixed size array object[] (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.

指尖凝香 2024-11-07 06:53:28

ArrayList 不存储对象,而仅存储对这些对象的引用。

An ArrayList does not store objects, but merely the references to those objects.

笑饮青盏花 2024-11-07 06:53:28

ArrayList 本质上是一个 object[] 的包装器,具有跟踪数组空间并根据需要将其增长(加倍)的功能。请注意,通常 List 是首选,但要回答这个问题:是的,它只存储对 object 的引用,这些对象很可能是装箱的值类型。

List 几乎相同,但围绕 T[],这意味着可以在不装箱的情况下存储值类型。引用类型仍然存储为引用。您还可以获得更多的类型安全性;即你不能添加错误的东西,也不能不正确地投射检索到的项目。

An ArrayList is essentially a wrapper around an object[], with functionality to track space in the array and grow it (double it) as necessary. Note that usually List<T> is preferred, but to answer the question: yes, it just stores the references to the objects, which may well be boxed value-types.

A List<T> is pretty much the same, but around a T[], 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.

書生途 2024-11-07 06:53:28

ArrayListList类似,是在 .NET 拥有泛型之前创建的。

它可以存储从 object 派生的任何内容,这些都是引用和值类型。所以你可以用它来存储对象列表。

ArrayList is similar to List<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.

恰似旧人归 2024-11-07 06:53:28

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 to object 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 the ArrayList.

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.

蘑菇王子 2024-11-07 06:53:28

它们存储任何对象

这意味着只要它们被实例化为对象,就可以用字符串、整数、类来填充它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文