这里介绍一些 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)
使用 C# 的 ArrayList
最新推荐文章于 2025-01-10 12:07:40 发布