ArrayList常用方法
1.构造函数
ArrayList list = new ArrayList();//空的ArrayList实例
2.添加元素
1.Add :
将一个对象添加到 ArrayList
的末尾,并返回新元素的索引
ArrayList list = new ArrayList();
list.Add(42); // 这里 int 值 42 会被装箱为 object
list.Add("Hello");
list.Add(true);
2.AddRange :
在 ArrayList
的末尾添加一个 ICollection
的元素
ArrayList arrayList1 = new ArrayList();
arrayList1.Add("Apple");
arrayList1.Add("Banana");
ArrayList arrayList2 = new ArrayList();
arrayList2.Add("Cherry");
arrayList2.Add("Date");
arrayList1.AddRange(arrayList2);
3.插入元素
1.insert :
将对象插入 ArrayList
的指定索引处
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
// 在下标1处插入"Cherry"
arrayList.Insert(1, "Cherry"); // Apple Cherry Banana
2.InsertRange :
将集合中的元素插入 ArrayList
的指定索引处
ArrayList arrayList1 = new ArrayList();
arrayList1.Add("Apple");
arrayList1.Add("Banana");
ArrayList arrayList2 = new ArrayList();
arrayList2.Add("Cherry");
arrayList2.Add("Date");
// 在索引1处插入arrayList2的所有元素
arrayList1.InsertRange(1, arrayList2);//Apple Cherry Date Banana
4.移除元素
1.Remove :
移除 ArrayList
中特定值的匹配项
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
arrayList.Add("Cherry");
// 移除 "Banana"
arrayList.Remove("Banana"); //Apple Cherry
2.RemoveAt :
移除 ArrayList
中指定索引处的元素
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
arrayList.Add("Cherry");
// 移除索引为1的元素(即 "Banana")
arrayList.RemoveAt(1); //Apple Cherry
3.RemoveRange :
移除 ArrayList
中从指定索引开始的指定数量的元素
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
arrayList.Add("Cherry");
arrayList.Add("Date");
arrayList.Add("Elderberry");
// 从索引1开始移除2个元素(即 "Banana" 和 "Cherry")
arrayList.RemoveRange(1, 2); //Apple Date Elderberry
4.Clear :
移除 ArrayList
中的所有元素
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
arrayList.Add("Cherry");
arrayList.Clear(); // 空
5.访问元素
1.object this[int index] :
通过索引访问或设置 ArrayList
中的元素
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
// 使用索引器读取元素
arrayList[0]; //Apple
2.Contains :
确定ArrayList
是否包含特定值
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
arrayList.Add("Cherry");
// 使用 Contains 方法检查 ArrayList 是否包含特定元素
arrayList.Contains("Apple")//True
3.IndexOf:
* ArrayList
*中特定值的第一个匹配项的索引
//如果没有找到返回-1
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
// 使用 IndexOf 方法查找第一个 "Apple" 的索引
arrayList.IndexOf("Apple"); //0
4.LastIndexOf:
* ArrayList
*中特定值的最后一个匹配项的索引
//如果没有找到返回-1
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
arrayList.Add("Apple"); // 重复的元素
arrayList.Add("Cherry");
// 使用 LastIndexOf 方法查找最后一个 "Apple" 的索引
arrayList.LastIndexOf("Apple"); //2
6.容量和大小
1.Capacity :
获取或设置ArrayList
可包含的元素数
ArrayList arrayList = new ArrayList();
arrayList.Capacity; // 初始容量,通常是16
arrayList.Capacity = 100; // 设置容量为100
2.Count :
获取ArrayList
中包含的元素数
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
arrayList.Count; // 2
3.TrimToSize:
* ArrayList
*的容量设置为列表中元素的实际数量
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
// 假设不再需要添加元素,并且希望释放未使用的内存
arrayList.TrimToSize();
arrayList.Capacity; // 2,因为现在容量与元素数量相等
7.排序和搜索
1.Sort()
对ArrayList
中的元素进行排序
ArrayList arrayList = new ArrayList();
arrayList.Add(3);
arrayList.Add(1);
arrayList.Add(2);
// 使用无参数的 Sort 方法进行排序
arrayList.Sort(); //1 2 3
2.Sort(IComparer comparer) :
使用指定的IComparer
接口对ArrayList
中的元素进行排序
ArrayList arrayList = new ArrayList();
arrayList.Add(3);
arrayList.Add(1);
arrayList.Add(2);
// 自定义比较器,用于数值排序
IComparer comparer = new CustomComparer();
// 使用自定义比较器进行排序
arrayList.Sort(comparer); //1 2 3
// 自定义比较器类
public class CustomComparer : IComparer
{
public int Compare(object x, object y)
{
if (x is IComparable && y is IComparable)
{
return ((IComparable)x).CompareTo(y);
}
throw new ArgumentException("Objects must be of type IComparable");
}
}
3.Reverse :
反转ArrayList
中元素的顺序
ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add(2);
arrayList.Add(3);
// 反转 ArrayList 中的元素顺序
arrayList.Reverse();//3 2 1
4.BinarySearch :
用二分查找算法搜索指定元素
//如果找到并返回其索引;如果未找到,则返回负值
ArrayList arrayList = new ArrayList();
// 假设arrayList已经按某种顺序排序
// ... 添加和排序元素 ...
int index = arrayList.BinarySearch("要搜索的值");
if (index >= 0)
{
Console.WriteLine("找到元素,索引为: " + index);
}
else
{
Console.WriteLine("未找到元素,应该插入的位置为: " + (~index));
}
//BinarySearch(int index, int count, object value)和BinarySearch(object value, IComparer comparer)不做解释