引入 声明
-
using System.Collections.Generic;
List<string> list = new List<string>();
删除
// 匹配删除所有满足条件的元素
List<string> list = new List<string>();
list.AddRange(new string[] { "Hello", "World", "Lee", "Lee", "Lee", "Lee", "Lee", "ZhangSan", "XiaoMing" });
list.RemoveAll(name => name == "Lee");
// list -> Hello World ZhangSan XiaoMing
其他方法
- 基础 List
List<string> list = new List<string>();
list.AddRange(new string[] { "Hello", "World", "Lee", "Lee", "Lee", "Lee", "Lee", "ZhangSan", "XiaoMing" });
- 从索引3向下取5个元素
// 从索引3向下取5个元素
List<string> newList = list.GetRange(3, 5); // Lee Lee Lee Lee ZhangSan
- 判断是否存在长度大于3的元素
// 判断是否存在长度大于3的元素
bool res = list.Exists(name => name.Length > 3);
Console.WriteLine(res); // True
- 查找第一个满足条件的元素
// 查找第一个满足条件的元素
string str = list.Find(name => name.Length > 6);
Console.WriteLine(str); // ZhangSan
- 查询List中所有满足条件的元素
// 查询List中所有满足条件的元素
List<string> newList = list.FindAll(name => name.Length > 6);
Console.WriteLine(newList); // ZhangSan XiaoMing
- 查询第一个满足条件的索引值
// 查询第一个满足条件的索引值
int index = list.FindIndex(name => name.Length > 6);
Console.WriteLine(index); // 7
- 查找最后一个满足条件的元素
// 查找最后一个满足条件的元素
string str = list.FindLast(name => name.Length > 6);
Console.WriteLine(str); // XiaoMing
- 查询最后一个满足条件的索引值
// 查询最后一个满足条件的索引值
int index = list.FindLastIndex(name => name.Length > 6);
Console.WriteLine(index); // 8
- 移除满足条件的元素
// 移除满足条件的元素
list.RemoveAll(name => name.Length < 6);
// list -> ZhangSan XiaoMing