C# list集合

一、list集合基本使用

1.添加元素

① 单个元素添加

cs 复制代码
  List<int> list = new List<int>();
  for (int i = 0; i < 3; i++)
  {
      list.Add(i);
  }
  //输出:0,1,2

②初始化时添加元素

cs 复制代码
  List<int> list2 = new List<int> { 1, 2, 3 };
  //输出:0,1,2

③集合中添加集合

cs 复制代码
  List<int> list2 = new List<int> { 1, 2, 3 };
  List<int> list3 = new List<int>();
 list3.AddRange(list2);
 //输出:0,1,2

2.删除元素

①删除集合中匹配项元素,未找到匹配元素,删除失败,返回false

cs 复制代码
 List<int> list = new List<int> { 1, 5, 7, 2, 8 };
 bool b= list.Remove(3); // 删除元素 3
 Console.WriteLine(b);  //false 删除失败
cs 复制代码
 List<int> list = new List<int> { 1, 2, 2, 2, 3 };
 bool b = list.Remove(3); // 删除元素 3
 Console.WriteLine(b);  //true 删除成功  输出:1,2,2,2

②删除集合中指定索引元素,索引从零开始

cs 复制代码
  List<int> list = new List<int> { 1, 2, 3, 4, 5 };
  list.RemoveAt(2); // 删除索引为 2 的元素,即元素 3  输出结果:1,2,4,5

③删除集合中一定范围内的元素,索引从零开始

cs 复制代码
  List<int> list = new List<int> { 1, 2, 3, 4, 5 };
  list.RemoveRange(0, 2); //删除一定范围内元素,从0开始,删除2个  输出结果:3,4,5

④删除集合中符合条件的数据

cs 复制代码
 List<int> list = new List<int> { 1, 2, 3, 4, 5 };
 list.RemoveAll(i => i % 2 == 0); // 删除所有偶数元素,输出结果1,3,5

3.改变集合中的元素 (不重要,不常用)

4.查找集合中的元素

①查找集合中匹配项元素,未找到匹配元素,删除-1,找到返回从0开始的索引

cs 复制代码
   List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
   int index = list.IndexOf(5); // 返回4  未找到返回-1

②根据指定的条件查找符合条件的所有元素,并返回一个新的List<T>集合

cs 复制代码
   List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
   List<int> newList = list.FindAll(x => x > 3); // 返回{ 4, 5 }
   

③使用Contains方法判断元素是否存在

cs 复制代码
  List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
  bool contains1 = list.Contains(3); // 返回true
  bool contains2 = list.Contains(6); // 返回false

二、list集合进阶使用

1、排序

①升序方法1

cs 复制代码
 List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };
 list.Sort();  //对集合内元素进行升序排列  输出:1,2,3,6,7,9

①升序方法2

cs 复制代码
   List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };
   list.Sort((a, b) => a.CompareTo(b)); //对集合内元素进行升序排列  输出:1,2,3,6,7,9

②降序方法1

cs 复制代码
  List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };
  list.Sort();  //先升序
  list.Reverse(); //翻转元素,达到降序的目的 输出:9,7,6,3,2,1

②降序方法2

cs 复制代码
  List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };
  list.Sort((x, y) => y.CompareTo(x)); // 降序 输出:9,7,6,3,2,1

2、求集合内全部元素平局值

cs 复制代码
 List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };
 double d= list.Average();  //计算集合内全部数据的平均值

3、求集合内全部元素之和

cs 复制代码
 List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };
 int i= list.Sum(); //计算集合内所有元素之和  输出:28

4、求集合内元素最大值

cs 复制代码
   List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };
   int i= list.Max(); //计算集合内所有元素中最大值  输出:9

5、求集合内元素最小值

cs 复制代码
  List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };
  int i = list.Min(); //计算集合内所有元素中最大值  输出:1

6、集合转数组

cs 复制代码
  List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };
  int[] ints = list.ToArray(); //list集合转为int类型数组

7、集合内元素去重

cs 复制代码
   List<int> list = new List<int>() { 7, 3, 6, 3, 6, 7 };
   list= list.Distinct().ToList(); //list集合内元素去重 输出:7,3,6

8、复制集合中指定索引长度元素至新集合中

cs 复制代码
  List<int> list = new List<int>() { 1, 3, 6, 2, 8, 7 };
  list= list.GetRange(0, list.Count-3); //从集合指定索引处,拷贝指定长度数量 存储在新集合中 输出:1,3,6

9、连接两个集合中全部元素生成新集合

cs 复制代码
   List<int> list1 = new List<int> { 1, 2, 3 };
   List<int> list2 = new List<int> { 4, 5, 6 };
   IEnumerable<int> combinedList = list1.Concat(list2);   //连接两个集合 ,生成IEnumerable类型
   int[] ints= combinedList.ToArray();  //因IEnumerable类型使用foreach遍历方便,为了使用for循环变量转为数组类型
   for (int i = 0; i < ints.Length; i++)
   {
       Console.WriteLine(ints[i]); //输出连接后的集合元素  输出结果:1,2,3,4,5,6
   }
相关推荐
多米Domi0111 小时前
0x3f 第48天 面向实习的八股背诵第五天 + 堆一题 背了JUC的题,java.util.Concurrency
开发语言·数据结构·python·算法·leetcode·面试
2301_822377651 小时前
模板元编程调试方法
开发语言·c++·算法
csbysj20201 小时前
Python 循环嵌套
开发语言
测试_AI_一辰1 小时前
Agent & RAG 测试工程05:把 RAG 的检索过程跑清楚:chunk 是什么、怎么来的、怎么被命中的
开发语言·人工智能·功能测试·自动化·ai编程
Coding茶水间1 小时前
基于深度学习的输电电力设备检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
开发语言·人工智能·深度学习·yolo·目标检测·机器学习
清风~徐~来1 小时前
【视频点播系统】BRpc 介绍及使用
开发语言
啟明起鸣1 小时前
【C++ 性能提升技巧】C++ 的引用、值类型、构造函数、移动语义与 noexcept 特性,可扩容的容器
开发语言·c++
故以往之不谏1 小时前
函数--值传递
开发语言·数据结构·c++·算法·学习方法
卢锡荣1 小时前
Type-c OTG数据与充电如何进行交互使用应用讲解
c语言·开发语言·计算机外设·电脑·音视频