C#中ArrayList类的使用

来源:互联网 发布:张子萱的淘宝店叫什么 编辑:程序博客网 时间:2024/05/16 11:36

ArrayList类

使用大小可按需要动态增加的数组实现IList接口

命名空间:System.Collections

程序集:mscorlib

语法:

public class ArrayList:IList, ICollection, IEnumerable, ICloneable

1.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 fox

Console.WriteLine( "The Queue initially contains the following:" );// jumped over the lazy dog

myAL.AddRange( myQueue );

Console.WriteLine( "The ArrayList now contains the following:" );//The quick brown fox jumped over the lazy dog

2.使用索引器进行访问:

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 );//5

Console.WriteLine( " Capacity : {0}", myAL1.Capacity );//16

myAL1.TrimToSize();

Console.WriteLine( " Count : {0}", myAL1.Count );//5

Console.WriteLine( " Capacity : {0}", myAL1.Capacity );//5

myAL1.Clear();

Console.WriteLine( " Count : {0}", myAL1.Count );//0

Console.WriteLine( " Capacity : {0}", myAL1.Capacity );//5

myAL1.TrimToSize();

Console.WriteLine( " Count : {0}", myAL1.Count );//0

Console.WriteLine( " Capacity : {0}", myAL1.Capacity );//16

4.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可以为NULL

6.2 ArrayList.InsertRange方法

将集合中的某个元素插入ArrayList的指定索引处

语法:public virtual void InsertRange(int index, ICollection c);

注意 集合本身不能为NULL,但它可以包含为NULL的元素

/*-----------------------------------------------------------------------------*/

ArrayList还有很多的属性和方法,这里就不一一赘述,可以查阅msdn


1 1
原创粉丝点击