c# 学习笔记 - 集合(List)

1.概论

1.1 List 特性

  1. 可通过索引访问的强类型列表,可以对列表进行搜索、排序和相关操作

1.2 .NET API

  API介绍
在这里插入图片描述
.NET API参考

2. 基本使用

2.1 样例

class Test{
    static void print(List<int> list) {
        for(int i = 0; i < list.Count; i ++) { // Count获取实际存储元素个数
            Console.WriteLine(list[i]); // 0 1 2 -- 读取操作
        }
    }

    static void Main(){
        List<int> list = new List<int>(3); // 集合
        for(int i = 0; i < list.Capacity; i++ ) list.Add(i); // Capacity 集合最大存储量, Add()元素添加
        print(list);
    }
}

3. 添加类操作

3.1 Insert、AddRange、

public void Insert (int index, T item);
-- 在指定索引 index 前插入元素 item

static void Main(string[] args){
    List<int> list = new List<int>();
    for(int i = 0; i < 3; i ++) 
        list.Add(i); // 1 2 3

    list.Insert(0, 100);
    for(int i = 0; i < list.Count; i ++)
        Console.Write(list[i] + " ");
}
// 100 0 1 2
public void AddRange (System.Collections.Generic.IEnumerable<T> collection);
-- 将实现了collection接口的集合添加到该集合末尾当中

static void Main(string[] args){
    List<int> list = new List<int>();
    for(int i = 0; i < 3; i ++) 
        list.Add(10);

    int[] x = new int[] { 1, 2, 3 };
    list.AddRange(x);

    for(int i = 0; i < list.Count ;i ++ ) 
        Console.Write(list[i] + " ");
}
// 10 10 10 1 2 3 

4. 删除类操作

4.1 Remove()

public bool Remove (T item);
1. 删除List集合当中第一次出现的item元素
2. 成功删除返回true,否则返回false

4.2 RemoveAt()

public void RemoveAt (int index);
1. 删除指定索引位置的元素(索引必须合法,否则报错)

5. 查找类操作

6. 排序类操作

6.1 Sort(Comparsion)

public void Sort (Comparison<T> comparison);
1. 按照指定的Comparison<T>, 对整个List<T>元素进行排序
2. public delegate int Comparison<in T>(T x, T y); Comparison是一个委托类型, 排序时只需要建立对应的比较	
   方法即可, 并且对于只需要使用一次的排序方法可以直接使用匿名类型方式书写.
namespace DelegateAppl {
    class TestDelegate {
        public static int Comparsion(int x, int y){
            return x < y ? -1 : 1; // 从小到大排序
        }

        static void Main(){
            List<int> list = new List<int>(10);
            for(int i = 5; i < 5 + 10; i ++ ) list.Add(i % 10); // 5 6 ...9 0 1 2 3 4

            list.Sort(Comparsion); // 实现对应委托类型完成比较
            for(int i = 0; i < list.Count; i++) 
                Console.Write(list[i] + " "); // 0 ~ 9
            Console.WriteLine();

            list.Sort(delegate (int x, int y) { // 匿名类型方式完成
                return x < y ? 1 : -1; // 从大到小排序
            });
            for(int i = 0; i < list.Count; i++) 
                Console.Write(list[i] + " "); // 9 ~ 0
        }
        
    }
}

7. 其他类操作

7.1 ToList、

public static System.Collections.Generic.List<TSource> ToList<TSource> 
(this System.Collections.Generic.IEnumerable<TSource> source);
--  这是一个扩展方法,对于所有继承了 IEnumerable 这个接口的接口或者实现类而言都会拥有此方法,此方法的
作用是将该接口实现类转换为对应的list集合。 
	从IEnumerable<T>创建List<T>static void Main(string[] args){
    int[] x = new int[] { 1, 3, 2, 9 };

    List<int> list = x.ToList();
    for(int i = 0; i < list.Count; i++)
        Console.Write(list[i] + " "); 
}
// 1 3 2 9
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

psudd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值