软件编程
位置:首页>> 软件编程>> C#编程>> c# List和Dictionary常用的操作

c# List和Dictionary常用的操作

作者:hello黄先森  发布时间:2023-06-14 15:44:32 

标签:c#,List,Dictionary

本文主要汇总了在开发过程中,使用List和Dictionary常用的方法,例如增、删、改、查、排序等等各种常用操作。

在平时的开发过程中,List和Dictionary是我们经常使用到的数据结构,而且由于本人记性又差有些方法长时间不用就都忘了,所以总结出此博客,用于记录和分享一下关于这两种数据结构的使用方法。

一、List

首先对于这些内容最权威和完整介绍的地方就是微软的官方文档,里面全面且详细的介绍了Lits的所有属性及方法,经常查看官方文档是一个非常好的习惯。本文将会总结出在日常开发过程中相对常用的一些方法同时会配上示例。

1.添加方法:

Add(T)---将对象添加到List结尾处。

AddRange(IEnumerable<T>)---将指定集合的元素添加到List末尾。

Insert(int32,T)---将元素T插入到List指定索引处。

InsertRange(int32,IEnumerable<T>)---将集合中的元素插入到List的指定索引处。

2.删除方法:

Clear()---从List中移除所有元素。

Remove(T)--从List中移除特定对象的第一个匹配项。(请注意值类型与引用类型)

RemoveAll(Preaicate<T>)---移除与指定谓词所定义的条件相匹配的所有元素。例:


static void Main(string[] args)
       {
           List<test> a = new List<test>();
           for (int i = 0; i < 10; i++)
           {
               a.Add(new test(i, "张先生"));
           }

a.RemoveAll(x => x.ID > 5);
       }

public class test
       {
           public test(int a, string b)
           {
               ID = a;
               Name = b;
           }
           public int ID { get; set; }
           public string Name { get; set; }
       }

结果:删除掉了ID大于5的全部元素。

RemoveAt(Int32)---移除List的指定索引处的元素。

RemoveRange(Int32,Int32)---从List中移除一系列元素。

3.搜索方法:

对于List的搜索方法建议使用Linq查询表达式或者Lambda表达式的方式,可以参考博文:C# LINQ查询表达式用法对应LAMBDA表达式

IndexOf(T)---搜索指定的对象,并返回整个List中第一个匹配项从零开始的索引。

IndexOf(T,Int32)---搜索指定对象并返回List中指定索引到最后一个元素这部分元素中第一个匹配项的从零开始的索引。

IndexOf(T,Int32,Int32)---搜索指定对象并返回List中从指定索引开始并包含指定元素的这部分元素中第一个匹配项的从零开始索引。

Find(Preaicate<T>)---搜索与指定谓词所定义的条件相匹配的元素,并返回整个List中的第一个匹配元素。

FindAll(Preaicate<T>)---搜索与指定谓词定义的条件匹配的所有元素。

FindIndex(Preaicate<T>)---搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中第一个匹配元素的从零开始的索引。

FindIndex(Int32,Preaicate<T>)---搜索与指定谓词所定义的条件相匹配的元素,并返回 List<T> 中从指定索引到最后一个元素的元素范围内第一个匹配项的从零开始的索引。

FindIndex(In32,Int32,Preaicate<T>)---搜索与指定谓词所定义的条件相匹配的一个元素,并返回 List<T> 中从指定的索引开始、包含指定元素个数的元素范围内第一个匹配项的从零开始的索引。例:


static void Main(string[] args)
       {
           List<test> a = new List<test>();
           for (int i = 0; i < 10; i++)
           {
               a.Add(new test(i, "张先生"));
           }
           int res = a.FindIndex(x => x.ID == 3);
           Console.WriteLine(res);
       }

public class test
       {
           public test(int a, string b)
           {
               ID = a;
               Name = b;
           }
           public int ID { get; set; }
           public string Name { get; set; }
       }

结果:控制台打印的结果为3。

4.修改方法:

对于当前List是支持通过索引的方式进行修改操作的,操作方式类似于数组。

5.排序方法:

请参考博文C#LINQ查询表达式用法对应LAMBDA表达式

Sort()---使用默认比较器对整个 List<T> 中的元素进行排序。

对于List<T>类型的List进行排序,如果想要使用Sort()方法的话,可以通过匿名委托的方式实现,个人建议实现排序功能使用Linq的方式最好。对于Sort方法可以进行一下了解。请参考如下代码


public MainWindow()
       {
           InitializeComponent();

List<test> a = new List<test>();

a.Add(new test()
           {
               a = 1,
               b = ""
           });
           a.Add(new test()
           {
               a = 4,
               b = ""
           });
           a.Add(new test()
           {
               a = 2,
               b = ""
           });
           a.Add(new test()
           {
               a = 3,
               b = ""
           });
           a.Add(new test()
           {
               a = 9,
               b = ""
           });
           a.Add(new test()
           {
               a = 7,
               b = ""
           });
           a.Add(new test()
           {
               a = 6,
               b = ""
           });

a.Sort((left, right) =>
           {
               if (left.a > right.a)
                   return 1;
               else if (left.a == right.a)
                   return 0;
               else
                   return -1;
           });
       }

class test
       {
           public int a;
           public string b;
       }

通过以上方式可以实现用Sort方法对List<T>类型进行排序。

6.其它方法:

Exists(Predicate<T>)---确定 List<T> 是否包含与指定谓词定义的条件匹配的元素。

Contains(T)---确定某元素是否在 List<T> 中。

以上两个方法使用代码如下:


public MainWindow()
       {
           InitializeComponent();

List<test> a = new List<test>();

a.Add(new test()
           {
               a = 1,
               b = ""
           });

a.Add(new test()
           {
               a = 4,
               b = ""
           });

var aa = new test()
           {
               a = 55,
               b = "11"
           };
           a.Add(aa);

var res1 = a.Exists(x => x.a == 55);
           var res2 = a.Contains(aa);
       }

class test
       {
           public int a;
           public string b;
       }

运行结果,res1=true;res2=true

二、Dictionary

 介绍最完整详细的仍然是官方文档。

1.添加方法

 Add(TKey,TValue)---将指定的键和值添加到字典中。

TryAdd(Tkey,TValue)---尝试将指定的键和值添加到字典中。

2.删除方法

Clear()---将所有键和值从 Dictionary<TKey,TValue> 中移除。

Remove(TKey)---从 Dictionary<TKey,TValue> 中移除所指定的键的值。

Remove(TKey,TVlue)---从 Dictionary<TKey,TValue> 中删除具有指定键的值,并将元素复制到 value 参数。

3.搜索方法

Item[TKey]---获取或设置与指定的键关联的值。

可以使用Where方法实现,代码如下:


Dictionary<string, int> a = new Dictionary<string, int>();
           a.Add("1", 1);
           a.Add("2", 2);
           a.Add("3", 3);
           a.Add("4", 4);
           var res = a.Where(x => x.Key == "3").ToDictionary(x=>x.Key,y=>y.Value);

运行结果res是字典类型"3",3

4.修改方法

Item[TKey]---获取或设置与指定的键关联的值。通常使用字典的Key来修改字典的Value

字典类型的Key一旦定义则无法修改,如果想修改只能删除重新添加。

5.排序方法

一般字典类型主要是使用Key来进行索引,所以对于字典这种类数据型进行排序是无意义的,所以字典本身也没有sort方法。

如果非要排序也是可以实现通过Linq即可实现,代码如下:


Dictionary<string, int> a = new Dictionary<string, int>();
           a.Add("4", 1);
           a.Add("1", 2);
           a.Add("3", 3);
           a.Add("5", 4);

var res1 = a.OrderBy(x=>x.Key).ToDictionary(x => x.Key, x => x.Value);
           var res2 = a.OrderByDescending(x=>x.Key).ToDictionary(x => x.Key, x => x.Value);

运行结果res1和res2分别实现按照字典key的升序排序和降序排序。

6.其它方法

ContainsKey(TKey)---确定是否 Dictionary<TKey,TValue> 包含指定键。

ContainsKey(TValue)---确定 Dictionary<TKey,TValue> 是否包含特定值。

来源:https://www.cnblogs.com/hellohxs/p/14468081.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com