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); // 删除元素 3
 Console.WriteLine(b);  //false 删除失败
 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

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

  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); // 返回true
  bool 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
   }

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值