开放的编程资料库

当前位置:我爱分享网 > C#教程 > 正文

C# 列表查找

在本文中,我们展示了如何使用Find、FindLast、FindAll、FindIndex和FindLastIndex方法在C#中查找元素。

C#列表是相同类型元素的集合。可以通过索引访问元素。

C#列表查找

Find方法返回与给定谓词匹配的第一个元素。

谓词是一个返回布尔值的单参数函数。

public T? Find (Predicate<T> match);

FindPredicate委托作为参数。

var words = new List<string> { "sky", "cup", "new", "war", "wrong",
    "crypto", "forest", "water", "cup" };

var vals = new List<int> { -2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1 };

string? e = words.Find(e => e.StartsWith("w"));
Console.WriteLine(e);

int n = vals.Find(e => e > 0);
Console.WriteLine(n);

在这个例子中,我们首先找到以’w’开头的单词和第一个大于零的元素。

string? e = words.Find(e => e.StartsWith("w"));

谓词是一个lambda表达式。

$ dotnet run
war
3

C#列表FindLast

FindLast方法返回与给定谓词匹配的最后一个元素。

public T? FindLast (Predicate<T> match);

这是函数的语法。

var words = new List<string> { "sky", "cup", "new", "war", "wrong",
    "crypto", "forest", "water", "cup" };

var vals = new List<int> { -2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1 };

string? e = words.FindLast(e => e.StartsWith("w"));
Console.WriteLine(e);

int n = vals.FindLast(e => e > 0);
Console.WriteLine(n);

在程序中,我们找到最后一个以’w’开头的单词和最后一个大于零的整数。

$ dotnet run
water
1

C#列表查找

FindAll方法检索与指定谓词定义的条件匹配的所有元素。

var words = new List<string> { "sky", "cup", "new", "war", "wrong",
    "crypto", "forest", "water", "cup" };

var vals = new List<int> { -2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1 };

List<string> res = words.FindAll(e => e.StartsWith("w"));
Console.WriteLine(string.Join(',', res));

List<int> res2 = vals.FindAll(e => e > 0);
Console.WriteLine(string.Join(',', res2));

程序找到所有以“w”开头的单词和所有大于零的整数。

$ dotnet run
war,wrong,water
3,1,2,1,4,2,1

C#列表查找索引

FindIndex方法返回与给定谓词匹配的第一个元素的索引。如果未找到匹配项,则返回-1。

public int FindIndex(Predicate<T> match);
public int FindIndex(int startIndex, Predicate<T> match);
public int FindIndex(int startIndex, int count, Predicate<T> match);

重载方法将起始索引和计数(要搜索的元素数)作为参数。

var words = new List<string> { "sky", "cup", "new", "war", "wrong",
    "crypto", "forest", "water", "cup" };

int n = words.FindIndex(e => e.StartsWith("w"));
Console.WriteLine($"index of the 1. word that starts with 'w' is {n}");

int n2 = words.FindIndex(5, e => e.StartsWith("w"));
Console.WriteLine($"index of the 1. word that starts with 'w' after index 5 is {n2}");

我们有一个单词列表。我们找到第一个以’w’开头的单词的索引,然后是索引5之后。

$ dotnet run
index of the 1. word that starts with 'w' is 3
index of the 1. word that starts with 'w' after index 5 is 7

C#列表FindLastIndex

FindLastIndex方法返回与给定谓词匹配的最后一个元素的索引。如果未找到匹配项,则返回-1。

public int FindLastIndex (Predicate<T> match);
public int FindLastIndex (int startIndex, Predicate<T> match);
public int FindLastIndex (int startIndex, int count, Predicate<T> match);

这是方法的语法。

var words = new List<string> { "sky", "cup", "new", "war", "wrong",
    "crypto", "forest", "water", "cup" };

int n = words.FindLastIndex(e => e.StartsWith("w"));
Console.WriteLine($"index of the last word that starts with 'w' is {n}");

int n2 = words.FindLastIndex(5, e => e.StartsWith("w"));
Console.WriteLine($"index of the last word that starts with 'w' after index 5 is {n2}");

在程序中,我们从末尾找到以’w’开头的最后一个单词的索引,然后在索引5之后。

$ dotnet run
index of the last word that starts with 'w' is 7
index of the last word that starts with 'w' after index 5 is 4

在本文中,我们展示了如何在C#中查找列表元素。

列出所有C#教程。

未经允许不得转载:我爱分享网 » C# 列表查找

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏