c# 中ArrayList常用方法

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)不做解释
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值