c#中ArrayList和List的常用方法

在 C# 中,ArrayListList<T>是常用的集合类型,它们提供了丰富的方法来操作列表。以下是它们的一些常用方法:

1. 创建列表

复制代码
using System;
using System.Collections;  // ArrayList
using System.Collections.Generic;  // List<T>
​
// 创建ArrayList(可存储任意类型)
ArrayList arrayList = new ArrayList();
​
// 创建泛型List(强类型,推荐使用)
List<string> list = new List<string>();

2. 添加元素

复制代码
// ArrayList(可添加任意类型)
arrayList.Add("apple");  // 添加字符串
arrayList.Add(123);      // 添加整数
arrayList.Add(true);     // 添加布尔值
​
// List<T>(只能添加指定类型)
list.Add("apple");
list.Add("banana");
list.Add("cherry");
​
// 在指定位置插入元素
list.Insert(1, "grape");  // 结果:[apple, grape, banana, cherry]

3. 访问元素

复制代码
// 通过索引访问(索引从0开始)
string first = list[0];  // apple
​
// 获取列表数量
int count = list.Count;  // 4
​
// 检查索引是否越界
if (index < list.Count) {
    Console.WriteLine(list[index]);
}

4. 修改元素

复制代码
// 修改指定位置的元素
list[1] = "orange";  // 结果:[apple, orange, banana, cherry]

5. 删除元素

复制代码
// 通过值删除(删除第一个匹配项)
list.Remove("banana");  // 结果:[apple, orange, cherry]
​
// 通过索引删除
list.RemoveAt(0);       // 结果:[orange, cherry]
​
// 删除范围内的元素
list.RemoveRange(0, 1); // 结果:[cherry]
​
// 清空列表
list.Clear();           // 结果:[]

6. 判断元素是否存在

复制代码
bool hasApple = list.Contains("apple");  // false

7. 遍历列表

复制代码
// 使用foreach循环
foreach (string item in list) {
    Console.WriteLine(item);
}
​
// 使用for循环
for (int i = 0; i < list.Count; i++) {
    Console.WriteLine(list[i]);
}
​
// 使用Lambda表达式(C# 3.0+)
list.ForEach(item => Console.WriteLine(item));

8. 转换为数组

复制代码
string[] array = list.ToArray();

9. 排序

复制代码
// 升序排序(元素需实现IComparable接口)
list.Sort();

// 降序排序
list.Sort((a, b) => b.CompareTo(a));

// 自定义排序(按字符串长度)
list.Sort((a, b) => a.Length.CompareTo(b.Length));

10. 查找元素

复制代码
// 查找第一个匹配项
string firstFruit = list.Find(x => x.StartsWith("a"));

// 查找所有匹配项
List<string> fruitsWithA = list.FindAll(x => x.Contains("a"));

// 查找元素索引
int index = list.FindIndex(x => x == "apple");

ArrayList 特有的方法

复制代码
// 转换ArrayList中的所有元素类型
ArrayList converted = new ArrayList();
foreach (object item in arrayList) {
    converted.Add(Convert.ToString(item));
}

// 检查ArrayList是否固定大小
bool isFixedSize = arrayList.IsFixedSize;  // false

List<T> 特有的方法

复制代码
// 添加多个元素
list.AddRange(new[] { "apple", "banana" });

// 反转列表顺序
list.Reverse();

// 判断所有元素是否满足条件
bool allLongNames = list.TrueForAll(x => x.Length > 3);

// 调整列表容量
list.Capacity = 100;  // 设置最小容量
list.TrimExcess();    // 释放多余容量

注意事项

  • ArrayList 非泛型:可以存储任意类型,但存在装箱 / 拆箱开销,且类型不安全。

  • List<T> 泛型:推荐使用,类型安全且性能更好。

  • 线程安全 :两者都不是线程安全的,如需线程安全可使用ConcurrentBag<T>或手动同步。

相关推荐
小职boy5 小时前
不忘初心Windows系统工具箱下载 - 系统优化与维护工具(v1.1)
windows·系统·电脑软件·工具箱
Mininglamp_27186 小时前
Claude Code 封禁中国开发者之后:本地 AI 编程工具的替代方案实测
开发语言·人工智能·windows·开源软件·ai-native
贾斯汀frank7 小时前
C# 15 类型系统改进:Union Types
开发语言·windows·c#
时代的狂8 小时前
如何理解 C# 的 async 和 await
c#·.netcore·async·await
YHHLAI9 小时前
Agent 智能体开发实战 · 第四课:完整工具集 —— 打造 AI 编程 Agent 的工具箱
人工智能·windows·microsoft
xiaoshuaishuai812 小时前
C# AI实现PR处理、单元测试
开发语言·c#·log4j
LONGZHIQIN12 小时前
C#基础复习笔记
开发语言·笔记·c#
精神底层12 小时前
Skill——提示词的系统化封装
windows·.net
技术不好的崎鸣同学13 小时前
Windows 命令提示符(CMD)恶意脚本分析篇
网络·windows·安全·系统安全
时代的狂14 小时前
如何理解 C#/.NET 的依赖注入与生命周期
c#·.net·依赖注入·控制反转