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
   }
相关推荐
学习使我变快乐1 小时前
C++:const成员
开发语言·c++
500了2 小时前
Kotlin基本知识
android·开发语言·kotlin
不知所云,4 小时前
qt cmake自定义资源目录,手动加载资源(图片, qss文件)
开发语言·qt
安冬的码畜日常4 小时前
【玩转 JS 函数式编程_006】2.2 小试牛刀:用函数式编程(FP)实现事件只触发一次
开发语言·前端·javascript·函数式编程·tdd·fp·jasmine
阑梦清川4 小时前
Java继承、final/protected说明、super/this辨析
java·开发语言
PythonFun5 小时前
Python批量下载PPT模块并实现自动解压
开发语言·python·powerpoint
Death2005 小时前
Qt 6 相比 Qt 5 的主要提升与更新
开发语言·c++·qt·交互·数据可视化
机器视觉知识推荐、就业指导5 小时前
使用Qt实现实时数据动态绘制的折线图示例
开发语言·qt
快乐就好ya6 小时前
Java多线程
java·开发语言
CS_GaoMing6 小时前
Centos7 JDK 多版本管理与 Maven 构建问题和注意!
java·开发语言·maven·centos7·java多版本