数组和 ArrayList 有什么区别? [英] What is difference between array and ArrayList?

查看:28
本文介绍了数组和 ArrayList 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是数组,什么是ArrayList?它们有什么区别?

What is an array and what is an ArrayList? What is the difference between them?

推荐答案

Array 是一种存储一组相同类型数据的高性能方式,因为每个元素都排在它的旁边记忆中的邻居.这允许非常快速的访问,因为 (a) 代码可以做一些数学运算并快速跳转到数组中的任何位置,以及 (b) 元素都组合在一起,因此它们往往同时在内存中(更少页错误和缓存未命中)..NET 中的数组实际上是一个类 (System.Array),但它是一种特殊类型的类,.NET 引擎 (CLR) 很好理解.因此,您可以使用标准的数组访问符号(文本语言),例如foo[3] = 99;

An Array is a high performance way of storing a group of data with the same type because each element is laid out next to it's neighbor in memory. This allows for very fast access because (a) the code can do a little math and jump quickly to any location in the array, and (b) the elements are all grouped together so they tend to be in memory at the same time (fewer page faults and cache misses). An array in .NET is actually a class (System.Array), but a special type of class that is well understood by the .NET engine (CLR). Because of this, you can standard array access notation (text languages) such as foo[3] = 99;

ArrayList 中,您正在处理一个集合..NET 中有多种类型的集合(请参阅 System.Collections 和 System.Collections.Specialized 命名空间),但它们的关键在于它们支持的接口(IEnumerable、ICollections、IList 等).如果您查看这些接口的定义,您会发现集合都是关于将事物组合在一起并提供访问它们的方法.但是,对于 ArrayList,如果您添加的元素超出内部数组的处理能力,则 ArrayList 会自动创建一个更大的数组并将旧数组复制到新数组中.

In ArrayList, however, you are dealing with a collection. There are several types of collections in .NET (see the System.Collections and System.Collections.Specialized namespaces), but the key thing about them is the interfaces they support (IEnumerable, ICollections, IList, etc). If you look at the definition of these interfaces, you see that collections are all about grouping things together and providing methods to access them.With an ArrayList, however, if you add one element more than the internal array can handle, the ArrayList automatically creates a larger array and copies the old array into the new array.

这篇关于数组和 ArrayList 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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