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

查看:522
本文介绍了array和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.

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

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