C#中ArrayList类的使用
来源:互联网 发布:张子萱的淘宝店叫什么 编辑:程序博客网 时间:2024/05/16 11:36
ArrayList类
使用大小可按需要动态增加的数组实现IList接口
命名空间:System.Collections程序集:mscorlib语法:public class ArrayList:IList, ICollection, IEnumerable, ICloneable1.ArrayList添加元素1.1 ArrayList.Add方法:语法:public virtual int Add(Object value)将对象添加到ArrayList的结尾处ArrayList myAL = new ArrayList();myAL.Add("Hello");myAL.Add(123);myAL.Add("!");myAL.Add("wang");这里需要注意的是ArrayList中可以接受数据类型不同的元素!ArrayList接受空引用作为有效值并且允许重复的元素!从Add方法的语法可以看到参数类型为Object,因此在执行ArrayList.Add时,需要进行装箱操作。1.2 ArrayList.AddRange方法:将ICollection的元素添加到ArrayList的末尾语法:public virtual void AddRange(ICollection c)ArrayList myAL0 = new ArrayList();myAL0.Add( "The" );myAL0.Add( "quick" );myAL0.Add( "brown" );myAL0.Add( "fox" );// Creates and initializes a new Queue.Queue myQueue = new Queue();myQueue.Enqueue( "jumped" );myQueue.Enqueue( "over" );myQueue.Enqueue( "the" );myQueue.Enqueue( "lazy" );myQueue.Enqueue( "dog" );Console.WriteLine( "The ArrayList initially contains the following:" );//The quick brown foxConsole.WriteLine( "The Queue initially contains the following:" );// jumped over the lazy dogmyAL.AddRange( myQueue );Console.WriteLine( "The ArrayList now contains the following:" );//The quick brown fox jumped over the lazy dog2.使用索引器进行访问:int i = (int)myAL[1];可以看到上诉语句进行了拆箱,myAL[1]的类型为object,需要把引用类型转化为值类型。3.ArrayList删除元素(Remove, RemoveAt, RemoveRange)3.1 ArrayList.Remove方法
从ArrayList中移除特定对象的第一个匹配项,注意是第一个(ArrayList中允许重复元素)。
语法:public virtual void Remove(Object obj)myAL.Remove(123);执行完上诉代码,已移除元素下面的元素将上移以占据空出的位置,即myAL[0] = "Hello", myAL[1] = "!", myAL[2] = "wang"myAL.Remove(456);执行完上诉代码,如果ArrayList不包含指定对象,则ArrayList保持不变,且不引发异常。3.2 ArrayList.RemoveAt方法移除ArrayList的指定索引处的元素语法:public virtual void RemoveAt(int index)myAL.RemoveAt(1);执行完上诉代码,已移除元素下面的元素将上移以占据空出的位置,即myAL[0] = "Hello", myAL[1] = "wang"如果索引小于0或者大于等于Count,则引发ArgumentOutOfRangeException异常。3.3 ArrayList.RemoveRange方法从ArrayList中移除一定范围的元素语法:public virtual void RemoveRange(int index, int count)myAL.Add("wu");myAL.Add("zhang");myAL.Add("liu");//此时myAL为 "Hello" "wang" "wu" "zhang" "liu"myAL.RemoveRange(1, 2);执行完上诉代码,myAL为 "Hello" "zhang" "liu"4.ArrayList.Capacity属性获取或设置ArrayList可包含的元素数语法:public virtual int Capacity{get; set; }可以通过调用TrimToSize或通过显示设置Capacity属性减少容量。4.1 ArrayList.TrimToSize方法将容量设置为ArrayList中元素的实际数目若要将ArrayList重置为它的初始状态,在调用TrimToSize之前调用Clear方法。修正空ArrayList会将ArrayList的容量设置为默认容量ArrayList myAL1 = new ArrayList();myAL1.Add( "The" );myAL1.Add( "quick" );myAL1.Add( "brown" );myAL1.Add( "fox" );myAL1.Add( "jumped" );Console.WriteLine( " Count : {0}", myAL1.Count );//5Console.WriteLine( " Capacity : {0}", myAL1.Capacity );//16myAL1.TrimToSize();Console.WriteLine( " Count : {0}", myAL1.Count );//5Console.WriteLine( " Capacity : {0}", myAL1.Capacity );//5myAL1.Clear();Console.WriteLine( " Count : {0}", myAL1.Count );//0Console.WriteLine( " Capacity : {0}", myAL1.Capacity );//5myAL1.TrimToSize();Console.WriteLine( " Count : {0}", myAL1.Count );//0Console.WriteLine( " Capacity : {0}", myAL1.Capacity );//164.2 ArrayList.Clear方法从ArrayList中移除所有元素语法:public virtual void Clear()注意:Count被设置为零,但是Capacity保持不变5.ArrayList.Count属性获取ArrayList中实际包含的元素数语法:public virtual int Count{get;}与Capacity的区别:Capacity是ArrayList可以存储的元素数。Count是ArrayList中的实际元素数。Capacity总是大于或等于Count。如果添加元素时Count超过Capacity,则通过在复制旧元素和添加新元素之前重新分配内部数组来使容量自动增加。6.ArrayList插入元素6.1 ArrayList.Insert方法将元素插入ArrayList的指定索引处语法:public virtual void Insert(int index, Object value);注意 要插入的value可以为NULL6.2 ArrayList.InsertRange方法将集合中的某个元素插入ArrayList的指定索引处语法:public virtual void InsertRange(int index, ICollection c);注意 集合本身不能为NULL,但它可以包含为NULL的元素/*-----------------------------------------------------------------------------*/
ArrayList还有很多的属性和方法,这里就不一一赘述,可以查阅msdn
1 1
- C#中ArrayList类的使用
- C# 中 ArrayList的使用
- C#中arraylist的使用
- C#中 ArrayList的使用
- C# ArrayList的使用
- 使用 C# 的 ArrayList
- C# ArrayList的使用
- C# ArrayList的使用
- 使用 C# 的 ArrayList
- 使用 C# 的 ArrayList
- C# ArrayList 的使用
- C#中ArrayList类的用法 - 工欲善其事必先利其器
- C#中ArrayList类的使用方法
- C#中ArrayList类的使用方法
- C#中ArrayList类的使用方法
- C#中ArrayList类的元素访问
- C#中ArrayList的用法
- C#中 ArrayList 的使用方法
- HDU 2016
- Android Environment.getExternalStorageState使用
- phoneGap for ios 安装环境搭建
- android TextView 显示字数的限制问题
- 读书笔记MoreEffectiveC++(6)
- C#中ArrayList类的使用
- spring——依赖注入
- md5 二进制数据
- HD1106 排序
- 矩阵求逆
- 学习总结连载二
- source insight 如何新建一个工程
- VPN的各种协议介绍
- OOA/OOD/OOP