C# ArrayList 类概述与基本特性
System.Collections.ArrayList 类是一种特殊的数组结构。借助于元素的添加与删除操作,能够动态地调整数组的长度。
一、优点
1. 具备自动调整大小的功能
2. 支持灵活的元素插入操作
3. 支持灵活的元素删除操作
二、局限性
与普通数组相比,其运行速度稍慢。
三、添加元素
1. public virtual int Add(object value);
将对象添加到 ArrayList 的末尾。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
此时内容为 abcde
2. public virtual void Insert(int index, object value);
将元素插入到 ArrayList 指定的索引位置。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Insert(0, "aa");
结果为 aaabcde
3. public virtual void InsertRange(int index, ICollection c);
将集合中的元素批量插入到 ArrayList 指定的索引处。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
ArrayList list2 = new ArrayList();
list2.Add("tt");
list2.Add("ttt");
aList.InsertRange(2, list2);
结果为 abtttttcde
四、删除操作
a) public virtual void Remove(object obj);
从 ArrayList 中移除特定对象的第一个匹配项,注意仅移除首次出现的位置。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");
结果为 bcde
2. public virtual void RemoveAt(int index);
移除 ArrayList 指定索引处的元素。
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);
结果为 bcde
3. public virtual void RemoveRange(int index, int count);
从 ArrayList 中移除一定范围的元素。Index 表示起始索引,count 表示从该索引开始移除的元素数量。
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveRange(1, 3);
结果为 ae
4. public virtual void Clear();
从 ArrayList 中移除所有元素。
五、排序操作
a) public virtual void Sort();
对 ArrayList 或其一部分中的元素进行排序。
ArrayList aList = new ArrayList();
aList.Add("e");
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
DropDownList1.DataSource = aList; // DropDownList DropDownList1;
DropDownList1.DataBind();
结果为 eabcd
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Sort(); // 排序
DropDownList1.DataSource = aList; // DropDownList DropDownList1;
DropDownList1.DataBind();
结果为 abcde
b) public virtual void Reverse();
将 ArrayList 或其一部分中元素的顺序反转。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Reverse(); // 反转
DropDownList1.DataSource = aList; // DropDownList DropDownList1;
DropDownList1.DataBind();
结果为 edcba
六、查找操作
a) public virtual int IndexOf(object);
b) public virtual int IndexOf(object, int);
c) public virtual int IndexOf(object, int, int);
返回 ArrayList 或其一部分中某个值的第一个匹配项的从零开始的索引。若未找到则返回 -1。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
int nIndex = aList.IndexOf("a"); // 1
nIndex = aList.IndexOf("p"); // 没找到,-1
d) public virtual int LastIndexOf(object);
e) public virtual int LastIndexOf(object, int);
f) public virtual int LastIndexOf(object, int, int);
返回 ArrayList 或其一部分中某个值的最后一个匹配项的从零开始的索引。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("a"); // 同 0
aList.Add("d");
aList.Add("e");
int nIndex = aList.LastIndexOf("a"); // 值为 2 而不是 0
g) public virtual bool Contains(object item);
确定某个元素是否在 ArrayList 中。包含则返回 true,否则返回 false。
七、其他属性与方法
1. public virtual int Capacity { get; set; }
获取或设置 ArrayList 可包含的元素数。
2. public virtual int Count { get; }
获取 ArrayList 中实际包含的元素数。
Capacity 是 ArrayList 可以存储的元素总数,Count 是 ArrayList 中实际包含的元素数。Capacity 总是大于或等于 Count。如果在添加元素时,Count 超过 Capacity,则该列表的容量会通过自动重新分配内部数组加倍。
如果 Capacity 的值显式设置,则内部数组也需要重新分配以容纳指定的容量。如果 Capacity 被显式设置为 0,则公共语言运行库将其设置为默认容量。默认容量为 16。
在调用 Clear 后,Count 为 0,而此时 Capacity 却是默认容量 16,而不是 0。
3. public virtual void TrimToSize();
将容量设置为 ArrayList 中元素的实际数量。
如果不向列表中添加新元素,则此方法可用于最小化列表的内存系统开销。
若要完全清除列表中的所有元素,请在调用 TrimToSize 之前调用 Clear 方法。截去空 ArrayList 会将 ArrayList 的容量设置为默认容量,而不是零。
ArrayList aList = new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e"); // Count=5, Capacity=16,
aList.TrimToSize(); // Count=Capacity=5;