§ ITPOW >> 文档 >> C#

使用 C# 的 ArrayList

作者:vkvi 来源:ITPOW(原创) 日期:2007-7-4

C# 不支持动态数组,用 ArrayList 可以实现动态数组的功能。


您还会喜欢:C# []、Array、List、ArrayList 区别简介

这里介绍一些 ArrayList 常用的代码,都是望名生义,不再举例。

获取元素值

object value = al[index]; //al 为 ArrayList 对象,一般需要再对 value 进行类型转换,比如:int n = (int)value;

设置元素值

al[index] = value; //al 为 ArrayList 对象,index 必须小于 Count

追加元素

int ArrayList.Add(object value) 返回添加的元素的索引

插入元素

void ArrayList.Insert(int index, object value)

删除元素

删除元素后,后面的元素会前移,但 Capacity 不会变化。

void ArrayList.Remove(object obj) 从前(索引 0)往后查找,删除找到的第一个和 obj 相同的元素
void ArrayList.RemoveAt(int index) 删除索引 index 对应的元素
void ArrayList.RemoveRange(int index, int count) 从索引 index 开始,删除 count 个元素

查找元素

int ArrayList.IndexOf(object value) 从前(索引 0)往后查找,返回找到的第一个和 obj 相同的元素的索引
int ArrayList.IndexOf(object value, int startIndex)
int ArrayList.IndexOf(object value, int startIndex, int count)
int ArrayList.LastIndexOf(object value) 从后往前(索引 0)查找,返回找到的第一个和 obj 相同的元素的索引
int ArrayList.LastIndexOf(object value, int startIndex)
int ArrayList.LastIndexOf(object value, int startIndex, int count)

相关阅读

相关文章