• C#集合


    概念

    数组是一组用来存储类型一样的连串数据,如果元素个数是动态的,数组就无法满足使用要求。这个时候就会用到集合。所以集合就是用来存储长度不固定的一连串数据,并且具有一定的逻辑性和数据存储结构。集合是同一类型元素(对象)的组合,本身也是一个类(对象),就像一个容器一样,(可以)装着它的元素。

    常见的集合

    队列、栈、链表、字典和集。List<T>就是一种泛型集合,且它与数组功能相当,长度可变。

    集合接口的和类型
    绝大多数的集合在System.Collections和System.Collections.Generic命名空间中都能够找到。一些特殊的集合则需要在System.Collections.Specialized命名空间内。线程安全的集合类位于System.Collections.Concurrent命名空间内。

    一般而言,集合大多都继承了一些接口以便实现共有的方法。常见的接口如下:


    IEnumerable<T>
    此接口通常可用来实现轮询功能。例如将foreach语句实现在集合上,就需要继承此接口。这个接口定义了方法GetEnumerator(),它返回实现IEnumratator接口的枚举。

    其他接口还有 ICollection<T>,IList<T>,ISet<T>,IDictionary<Tkey,Tvalue>,ILookup<Tkey,Tvalue>,IComparer<T>,IEqualityComparer<T>,IProducerConsumerCollection<T>等等......

    List<T>初窥

    //所有泛型集合都在该命名空间下
    using System.Collections.Generic;

    初始化方法

                List<string> student = new List<string>();
                List<string> student = new List<string> { "wf", "xr", "jym" };

    对比数组和集合:

        int[] ages = new int[] { 16, 23, 25, 22 };
        List<int> ages = new List<int> { 16, 23, 25, 23, 22 };

    增删改查:

                student.Add("xm");
                student.Insert(3, "xm");
    
                student.Remove("xr");
                student.RemoveAt(0);
                student.clear();
    
                student[0] = "dfg";
    
                Console.WriteLine(student.BinarySearch("dfg"));
                Console.WriteLine(student.IndexOf("jym"));
                Console.WriteLine(student.Contains("jym"));

    文章转载自:软件开发平台
    地址:https://www.hocode.com/

  • 相关阅读:
    jq 比较五获取结构树的位置 eq() 或者parent
    jq 笔记四 end的用法
    jq 笔记三 find的用法
    jq 笔记二filter()用法
    jq 笔记一 选择器中的逗号
    Linux 笔记五 搜索命令的命令
    linux 笔记四 文件搜索(locate)
    使用ansible时显示Failed to connect to the host via ssh
    linux音频解码软件ffmpeg的安装
    centos服务器jenkins安装配置
  • 原文地址:https://www.cnblogs.com/frfwef/p/12762572.html
Copyright © 2020-2023  润新知