C#中的ArrayList
来源:互联网 发布:linux 改变所属组 编辑:程序博客网 时间:2024/06/14 03:52
ArrayList非常类似于数字,也称为数组列表,ArrayList是可以非常直观地动态维护的。数组的容量是固定的,ArrayList的容量可以根据需要自动扩充,它的索引会根据扩展而重新进行分配和调整。ArrayList可以根据类提供的方法,进行访问、新增、删除元素的操作,实现对集合的动态访问。
使用大小可按需动态增加的数组实现 IList 接口。ArrayList类来自于System.Collections命名空间,在使用ArrayList之前一定要引入这个命名空间。注意ArrayList是动态可维护的,在第一时可以不指定容量,也可以指定容量。
ArrayList myAL1 = new ArrayList(5);
注意:
不保证会对 ArrayList 排序。 在执行需要对 ArrayList 排序的操作之前,必须对 ArrayList 进行排序。
ArrayList 的容量是 ArrayList 可以保存的元素数。随着向 ArrayList 中添加元素,容量通过重新分配按需自动增加。可通过调用 TrimToSize 或通过显式设置 Capacity 属性减少容量。
使用整数索引可以访问此集合中的元素。 此集合中的索引从零开始。
ArrayList 接受 null引用作为有效值并且允许重复的元素。
ArrayList 只提供一维的形式。
ArrayList.Capacity 属性:获取或设置 ArrayList 可包含的元素数。
ArrayList.Count 属性:获取 ArrayList 中实际包含的元素数。
Capacity 是 ArrayList 可以存储的元素数。Count 是 ArrayList 中实际存储的元素数。
Capacity 总是大于或等于 Count。如果在添加元素时 Count 超过 Capacity,则通过在复制旧元素和添加新元素之前重新分配内部数组来使容量自动增加。增长后的容量是原来的两倍。
可通过调用 TrimToSize 或通过显式设置 Capacity 属性减少容量。如果 Capacity 的值显式设置,则内部数组也需要重新分配以容纳指定的容量。
检索此属性值的运算复杂度为 O(1);设置此属性的运算复杂度为 O(n),其中,n 是新容量。
ArrayList.Item 属性:获取或设置指定索引处的元素。
参数
index
类型:System.Int32
要获得或设置的元素从零开始的索引。
属性值
类型:SystemObject
指定索引处的元素。
注意:
ArrayList 接受 null 引用作为有效值并且允许重复的元素。
通过使用下面的语法,此属性提供访问集合中特定元素的能力:myCollection[index]。
C# 语言使用 this 关键字来定义索引器而不是通过实现 Item 属性。
检索此属性值的运算复杂度为 O(1);设置此属性的运算复杂度也是 O(1)。
Console.WriteLine("Element {0} is /"{1}/"", i, stringList[i]);
}
ArrayList.Add 方法:将对象添加到 ArrayList 的结尾处。
参数
value
类型:SystemObject
要添加到 ArrayList 的末尾处的 Object。该值可以为 null 引用。
返回值
类型:System.Int32
ArrayList 索引,已在此处添加了 value。
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
ArrayList.AddRange 方法:将 ICollection 的元素添加到 ArrayList 的末尾。
参数
c
类型:System.Collections.ICollection
ICollection,其元素应被添加到 ArrayList 的末尾。集合本身不能为 null引用,但它可以包含为 null 引用的元素。
// Copies the Queue elements to the end of the ArrayList.
ArrayList.Insert 方法:将元素插入 ArrayList 的指定索引处。
参数
index
类型:System.Int32
从零开始的索引,应在该位置插入 value。
value
类型:System.Object
要插入的 Object。该值可以为 null 引用。
myAL.Insert( 2, "jumps" );
myAL.Insert( 3, "over" );
myAL.Insert( 4, "the" );
myAL.Insert( 5, "dog" );
ArrayList.InsertRange 方法:将 ICollection 的元素添加到 ArrayList 的末尾。
参数
index
类型:System.Int32
从零开始的索引,应在该位置插入 value。
c
类型:System.Collections.ICollection
ICollection,应将其元素插入到 ArrayList 中。集合本身不能为 null 引用,但它可以包含为 null 引用的元素。
myQueue.Enqueue( "quick" );
myQueue.Enqueue( "brown" );
// Copies the Queue elements to the ArrayList at index 1.
myAL.InsertRange( 1, myQueue );
ArrayList.Remove 方法:从 ArrayList 中移除特定对象的第一个匹配项。
参数
obj
类型:System.Object
要从 ArrayList 移除的 Object。该值可以为 null 引用。
注意:
如果 ArrayList 不包含指定对象,则 ArrayList 保持不变。不引发异常。
ArrayList.RemoveRange 方法:从 ArrayList 中移除一定范围的元素。
参数
index
类型:System.Int32
要移除的元素的从零开始的索引。
count
类型:System.Int32
要移除的元素数
myAL.RemoveRange( 4, 3 );
ArrayList.Clear方法:从 ArrayList 中移除所有元素。
Count 被设置为零,并且对来自该集合元素的其他对象的引用也被释放。
Capacity 保持不变。
myAL.Clear();
- c# 中的 ArrayList
- 关于C#中的ArrayList
- C#中的ArrayList
- C#中的ArrayList类
- C#中的Array和ArrayList
- 关于c#中的ArrayList数组
- C#中的数组,ArrayList,List
- C#中的列表List与ArrayList
- C#中的动态列表(ArrayList)
- C# ArrayList
- C# ArrayList
- c# ArrayList
- C#如何查找ArrayList 中的某个数据?(附解决方案及源码)
- C#中的列表结合List与ArrayList介绍
- c#中的排序算法(动态数组ArrayList)
- c#中的排序算法(动态数组ArrayList)
- c#中的ArrayList属性祥解及其语法…
- 从C#中的ArrayList和List的区别说起
- Python 学习资源 ( 整理日期2010-02-24 )
- 雅虎网站页面性能优化的34条黄金守则
- 成败的关键在于能否控制行为
- 通过vc的观察窗口调程序
- GridView 72般绝技 (完整的GridView应用)
- C#中的ArrayList
- javascript 命名空间
- dumpbin命令不能使用解决方法
- intents对象说明
- 推荐 Eclipse 阅读清单
- _cdecl 和_stdcall
- 离线部署额外域控制器,Active Directory系列之六
- js变态 地址栏录入网页上的图片旋转
- zgrep