当前位置: 首页 > news >正文

C# list集合

一、list集合基本使用

1.添加元素

① 单个元素添加

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

②初始化时添加元素 

  List<int> list2 = new List<int> { 1, 2, 3 };//输出:0,1,2

③集合中添加集合 

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

2.删除元素

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

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

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

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

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

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

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

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

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

4.查找集合中的元素

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

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

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

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

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

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

二、list集合进阶使用

1、排序

①升序方法1

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

①升序方法2

   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

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

②降序方法2

  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、求集合内全部元素平局值

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

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

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

4、求集合内元素最大值

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

5、求集合内元素最小值

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

6、集合转数组

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

7、集合内元素去重

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

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

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

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

   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}

相关文章:

  • 卷积神经网络-奥特曼识别
  • # 详解 JS 中的事件循环、宏/微任务、Primise对象、定时器函数,以及其在工作中的应用和注意事项
  • Swoole 协程调度机制
  • 构建 VPC 并启动 Web 服务器
  • ADB安装教程
  • 修改python环境包的内容后如何及时更新到内存
  • 零拷贝(Zero Copy)
  • 动态分配函数参数用二级指针的作用
  • 将 cuda kernel 编译成 ptx 和 rocm的hip asm
  • CentOS7.9部署安装OpenGauss 5.0.2企业版
  • 【源码】Spring Data JPA原理解析之Repository自定义方法添加@Query注解的执行原理
  • 木叶飞舞之【机器人ROS2】篇章_第三节、给turtlebot3安装realsense深度相机
  • 大语言模型应用与传统程序的不同
  • R可视化:另类的柱状图
  • 生信分析进阶3 - pysam操作bam文件统计unique reads和mapped reads高级技巧合辑
  • [译]CSS 居中(Center)方法大合集
  • 2017 前端面试准备 - 收藏集 - 掘金
  • Android单元测试 - 几个重要问题
  • avalon2.2的VM生成过程
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • JAVA_NIO系列——Channel和Buffer详解
  • java8 Stream Pipelines 浅析
  • JavaScript 基础知识 - 入门篇(一)
  • JavaScript服务器推送技术之 WebSocket
  • JavaScript设计模式之工厂模式
  • JavaScript学习总结——原型
  • Js基础——数据类型之Null和Undefined
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • PHP的类修饰符与访问修饰符
  • puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决
  • 阿里云购买磁盘后挂载
  • 搞机器学习要哪些技能
  • 如何在 Tornado 中实现 Middleware
  • 移动端唤起键盘时取消position:fixed定位
  • 应用生命周期终极 DevOps 工具包
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • 智能合约开发环境搭建及Hello World合约
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • Spring Batch JSON 支持
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • ### RabbitMQ五种工作模式:
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (八)Flask之app.route装饰器函数的参数
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (一)Neo4j下载安装以及初次使用
  • (转)jQuery 基础
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .NET技术成长路线架构图
  • @PostConstruct 注解的方法用于资源的初始化
  • [2016.7 day.5] T2
  • [5] CUDA线程调用与存储器架构
  • [Angular] 笔记 9:list/detail 页面以及@Output
  • [autojs]逍遥模拟器和vscode对接
  • [BZOJ 1032][JSOI2007]祖码Zuma(区间Dp)