广告位联系
返回顶部
分享到

C#列表List<T>、HashSet和只读集合介绍

C#教程 来源:互联网 作者:酷站 发布时间:2022-05-09 22:45:24 人浏览
摘要

一、概述 ListT 是ArrayList类的等效泛型类。属System.Collections.Generic命名空间。 二、声明及初始化 1、ListT mList=new ListT([capacity]) :可带初始容量 1 Liststring mList=new Liststring(); 2、ListT mList=new

一、概述

List<T> 是ArrayList类的等效泛型类。属System.Collections.Generic命名空间。

二、声明及初始化

1、List<T> mList=new List<T>([capacity]) :可带初始容量

1

List<string> mList=new List<string>();

2、List<T> mList=new List<T><IEnumerable<T> collection): 从指定集合(如数组)复制元素进行初始化。

1

2

3

List<string> mList0 = new List<string>(new []{ "a", "b", "c" } );//数组

List<string> mList1 = new List<string>("a,b,c".Split(new char[] {','}) );//将字符串转成List<string>

List<string> mList2 = new List<string> { "a", "b", "c" };//集合初始值设定项

三、常用属性和方法

1、添加元素

1、添加一个元素:

1

mList.Add("a");

2、添加一组元素

1

mList.AddRange(new [] { new Person("a", 20), new Person("b", 10), }

3、在Index处插入一个元素

1

mList.Insert(0,"d");//以前占此及此位以后的元素都往后移动

4、在Index处插入一组元素:

1

mList.InsertRange(0,new []{"E","f"});

2、删除元素

1、删除一个值:

1

mList.Remove("a");

2、删除索引处的元素:

1

2

3

mList.RemoveAt(0);

for (int i = mList.Count - 1; i >= 0; i--)

{ }

3、删除范围内的元素:

1

mList.RemoveRange(3, 2);//index,count

4、清空所有元素:

1

mList.Clear();

5、删除与指定条件匹配的所有元素:

1

mList.RemoveAll(p => p.Length > 1);//bool Predicate<T>(T matach)  委托

注意:Remove()、Contais()、IndexOf、LastIndexOf()方法需要使用“相等”比较器。 
List<T>中的元素实现了IEquatable接口则使用其Equals()方法,否则默认使用Object.Equals(object)。

3、访问列表元素以及遍历列表:

1、用索引的形式访问

1

mList[0]

2、遍历

1

2

3

4

5

6

7

8

9

foreach (string s in mList)

{

    Console.WriteLine(s);

}

 

for (int i = 0; i < mList.Count; i++)

{

    Console.WriteLine(s);

}

注意:Count属性:实际包含的元素数;

Capacity:能够容纳的元素总数;

TrimExcess()方法可将容量调整为实际容量。

4、判断元素存在:

1、判断整个元素是否存在该List中:

1

2

if (mList.Contains("d"))

    { }

2、判断是否存在于指定条件匹配的元素:

1

2

if (mList.Exists(p => p.Length > 2))

    { }

3、判读是否List中每个元素都与指定条件匹配:

1

2

if (mList.TrueForAll(p => p.Length > 2))

 {}

5、搜索:

查找索引

1、查找列表中某个项的第一个索引:

1

mList.IndexOf(T,[index],[count]);

2、查找某元素在列表中最后一个匹配 项的索引:

1

mList.LastIndexOf(T,[index],[count]);

3、查找与指定条件匹配的元素在列表中第一个匹配项的索引:

1

mList.FindIndex([startIndex],[count],match);

4、查找与指定条件匹配的元素在列表中最后一个匹配项的索引:

1

mList.FindLastIndex(2, 10, p => p.Length > 2);

查找元素:

1、查找与指定条件匹配的元素,返回第一个匹配元素:

1

string find= mList.Find( p => p.Length > 2);

2、查找与指定条件匹配的元素,返回最后一个匹配元素:

1

string find= mList.FindLast( p => p.Length > 2);

3、查找并返回与指定条件匹配的元素列表:

1

List<string> finds= mList.FindAll( p => p.Length > 2);

6、排序:

委托签名:

1

int Comparison<T>(T x, T y);

1、使用Comparision<T>委托进行元素排序:

1

2

3

4

5

6

mList.Sort((x, y) =>

    {

        int result = x[0].CompareTo(y[0]);

        if (result == 0) { return x[1].CompareTo(y[1]); }

        return result;

    });

2、顺序翻转

1

mList.Reverse()//index,count

注意:Sort方法还可以利用ICompable和Icomparer接口排序。

7、转换:

1、将一定范围内的元素从List<T>复制到兼容的一维数组中。

1

mList.CopyTo([index],array,[arrryIndex],[count]);

2、将List<T>中的元素复制到新数组中。

1

string[] arr=mList.ToArray();

3、创建源List<T>的元素范围的浅表副本。

1

List(string) range= mList.GetRange(inex,count);

4、将当前List<T>的元素转换成另一种类型,返回转换后元素的列表

1

List<char> chars=mList.ConvertAll(p=>p[0]);

1

List<Racer> RacerList=PList.ConvertAll<Person,Racer>(p=>new Racer(p.FirstName+" " +p.LastName));

Converter委托签名:

1

TOutput Converter<in TInput,out TOutput>(TInput input);

5、将List<string>转成用逗号分隔的字符串

1

string  aa= String.Join(",",mList)

8、去掉重复项(Distinct)

需要引用using System.Linq;

1、默认比较器:

1

mList.Distinct().ToList();

2、自定义比较器

1

2

3

4

5

6

7

8

9

10

11

12

13

mList.Distinct(new MyComparer()).ToList();

public class MyComparer : System.Collections.Generic.IEqualityComparer<string>

{

    public bool Equals(string x, string y)

    {

        return x.ToUpper()==y.ToUpper();

    }

 

    public int GetHashCode(string obj)

    {

        return obj.ToUpper().GetHashCode();

    }

}

9、只读集合

List的AsReadOnly()方法返回ReadOnlyCollection,所有修改方法抛出NotSupportedException异常。

1

System.Collections.ObjectModel.ReadOnlyCollection readOnlyList= mList.AsReadOnly();

四:HashSet<T>

   用来存储集合,基于Hash,可理解为没有Value,只有Key的Dictionary<TKey,TValue);

  • HashSet<T>不能使用索引访问,不能存储重复数据,元素T必须实现了Equals和GetHashCode方法;
  • HashSet<T>的检索性能比List<T>好得多。如Contains(),Remove();
  • HashSet<T>是专门用来和集合运算(取并集、交集等),所以提供了UnionWith(),InterSetWith()等方法。

1、常用方法

Add、IsSubsetOf、IsSupersetOf、Overlaps、UnionWith

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

var companyTeams = new HashSet<string> { "Ferrari", "McLaren", "Mercedes" };//公司队

var traditionalTeams = new HashSet<string> { "Ferrari", "McLaren" };//传统队

var privateTeams = new HashSet<string> { "Red Bull", "Toro Rosso", "Force India", "Sauber" };//私有队

 

//Add方法,将一个元素添加到集中,成功返回true失败返回false

if (privateTeams.Add("Williams"))//返回true

    Console.WriteLine("Williams Add Success.privateTeams count:{0}", privateTeams.Count);

if (!companyTeams.Add("McLaren"))//返回false

    Console.WriteLine("McLaren Add Failure.companyTeams count:{0}", companyTeams.Count);

 

//IsSubset,确认(traditionalTeams)对象是否为指定集(companyTeams)的子集

if (traditionalTeams.IsSubsetOf(companyTeams))//traditionalTeams是companyTeams的子集,返回true

    Console.WriteLine("traditionalTeams is sub of companyTeams.");

 

//IsSuperset,确认(companyTeams)对象是否为指定集(traditionalTeams)的超集(父集)

if (companyTeams.IsSupersetOf(traditionalTeams))//companyTeams是traditionalTeams的父集,返回true

    Console.WriteLine("companyTeams is a superset of traditionalTeams");

 

//Overlaps,确认对象(privateTeams)和指定集(traditionalTeams)是否存在共同元素

traditionalTeams.Add("Williams");

if (privateTeams.Overlaps(traditionalTeams))//privateTeams和traditionalTeams都包含有Williams的元素,返回true

    Console.WriteLine("At least one team is the same with the traditionalTeams and privateTeams.");

 

//UnionWith,将指定对象元素添加到当前对象(allTeams)中,因为对象类型为SortedSet,所以是元素是唯一有序的

var allTeams = new SortedSet<string>();

allTeams.UnionWith(companyTeams);

allTeams.UnionWith(traditionalTeams);

allTeams.UnionWith(privateTeams);

foreach (var item in allTeams)

{

    Console.Write(item);

}

2、HashSet和SortedSet的区别

共同点:  
1. 都是集,都具有集的特征,包含的元素不能有重复

不同点:  
1. HashSet的元素是无序的,SortedSet的元素是有序的

五、链表 LinkedList

链表是一串存储数据的链式数据结构,它的每个成员都有额外的两个空间来关联它的上一个成员和下一个成员。

所以,链表对于插入和删除操作效率会高于ArrayList,因为它存储了上一个成员和下一个成员的指针,进行插入和删除只需要改变当前LinkedListNode的Previous和Next的指向即可。

1、链表的内存表视图

2、实例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

static void Main(string[] args)

{

    LinkedList<Document> linkedlist = new LinkedList<Document>();

    //添加节点

    linkedlist.AddFirst(new Document("1"));

    linkedlist.AddFirst(new Document("2"));

    Display(linkedlist); //title:2   title:1

 

    Document doc = new Document("3");

    linkedlist.AddLast(doc);

    Document doc1 = new Document("4");

    linkedlist.AddLast(doc1);

    Display(linkedlist); //title:2   title:1  title:3   title:4

    //查找节点

    LinkedListNode<Document> findnode = linkedlist.FindLast(doc);

    Display(findnode.Value); //title:3

 

    //插入节点

    linkedlist.AddBefore(findnode, new Document("5"));

    Display(linkedlist); //title:2   title:1  title:5   title:3   title:4

 

    linkedlist.AddAfter(findnode, new Document("6"));

    Display(linkedlist); //title:2   title:1  title:5   title:3  title:6  title:4

 

    linkedlist.AddAfter(findnode.Previous, new Document("7"));

    Display(linkedlist); //title:2   title:1  title:5   title:7  title:3  title:6  title:4

 

    linkedlist.AddAfter(findnode.Next, new Document("8"));

    Display(linkedlist); //title:2   title:1  title:5   title:7  title:3  title:6  title:8  title:4

 

    //移除节点

    linkedlist.Remove(findnode);

    Display(linkedlist); //title:2   title:1  title:5   title:7  title:6  title:8  title:4

 

    linkedlist.Remove(doc1);

    Display(linkedlist); //title:2   title:1  title:5   title:7  title:6  title:8

 

    linkedlist.RemoveFirst();

    Display(linkedlist); //title:1  title:5   title:7  title:6  title:8

 

    linkedlist.RemoveLast();

    Display(linkedlist); //title:1  title:5   title:7  title:6

}

 

private static void Display<T>(LinkedList<T> linkedlist)

{

    foreach (var item in linkedlist)

    {

        Console.Write(item + " ");

    }

}

 

private static void Display<T>(T doc)

{

    Console.Write(doc);

}

 

public class Document

{

    public string title { get; private set; }

    public Document(string title)

    {

        this.title = title;

    }

 

    public override string ToString()

    {

        return string.Format("title:{0}", title);

    }

}


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://www.cnblogs.com/springsnow/p/9428516.html
相关文章
  • WPF实现窗体亚克力效果的代码

    WPF实现窗体亚克力效果的代码
    WPF 窗体设置亚克力效果 框架使用大于等于.NET40。 Visual Studio 2022。 项目使用MIT开源许可协议。 WindowAcrylicBlur设置亚克力颜色。 Opacity设置透
  • C#非托管泄漏中HEAP_ENTRY的Size对不上解析

    C#非托管泄漏中HEAP_ENTRY的Size对不上解析
    一:背景 1. 讲故事 前段时间有位朋友在分析他的非托管泄漏时,发现NT堆的_HEAP_ENTRY的 Size 和!heap命令中的 Size 对不上,来咨询是怎么回事?
  • C#中ArrayList 类的使用介绍
    一:ArrayList 类简单说明 动态数组ArrayList类在System.Collecions的命名空间下,所以使用时要加入System.Collecions命名空间,而且ArrayList提供添加,
  • C#使用BinaryFormatter类、ISerializable接口、XmlSeriali

    C#使用BinaryFormatter类、ISerializable接口、XmlSeriali
    序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程。对象一旦被序列化,就可以把对象状态保存到硬盘的某个位
  • C#序列化与反序列化集合对象并进行版本控制
    当涉及到跨进程甚至是跨域传输数据的时候,我们需要把对象序列化和反序列化。 首先可以使用Serializable特性。 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  • C#事件中关于sender的用法解读

    C#事件中关于sender的用法解读
    C#事件sender的小用法 开WPF新坑了,看了WPF的炫酷界面,再看看winForm实在是有些惨不忍睹(逃)。后面会开始写一些短的学习笔记。 一、什么
  • 在C#程序中注入恶意DLL的方法

    在C#程序中注入恶意DLL的方法
    一、背景 前段时间在训练营上课的时候就有朋友提到一个问题,为什么 Windbg 附加到 C# 程序后,程序就处于中断状态了?它到底是如何实现
  • 基于C#实现一个简单的FTP操作工具
    实现功能 实现使用FTP上传、下载、重命名、刷新、删除功能 开发环境 开发工具: Visual Studio 2013 .NET Framework版本:4.5 实现代码 1 2 3 4 5 6 7
  • C#仿QQ实现简单的截图功能

    C#仿QQ实现简单的截图功能
    接上一篇写的截取电脑屏幕,我们在原来的基础上加一个选择区域的功能,实现自定义选择截图。 个人比较懒,上一篇的代码就不重新设计
  • C#实现线性查找算法的介绍
    线性查找,肯定是以线性的方式,在集合或数组中查找某个元素。 通过代码来理解线性查找 什么叫线性?还是在代码中体会吧。 首先需要一
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计