Array.FindLast<T>(T[], Predicate<T>) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
搜索与指定谓词定义的条件匹配的元素,并返回整个 Array中的最后一个匹配项。
public:
generic <typename T>
static T FindLast(cli::array <T> ^ array, Predicate<T> ^ match);
public static T FindLast<T> (T[] array, Predicate<T> match);
public static T? FindLast<T> (T[] array, Predicate<T> match);
static member FindLast : 'T[] * Predicate<'T> -> 'T
Public Shared Function FindLast(Of T) (array As T(), match As Predicate(Of T)) As T
类型参数
- T
数组元素的类型。
参数
- array
- T[]
要搜索的从零开始的一维 Array。
- match
- Predicate<T>
定义要搜索的元素条件的 Predicate<T>。
返回
与指定谓词定义的条件匹配的最后一个元素(如果找到);否则,类型为 T
的默认值。
例外
示例
下面的代码示例演示 Find、FindLast和 FindAll 泛型方法。 将创建一个字符串数组,其中包含 8 个恐龙名称,其中两个名称(在位置 1 和 5)以“saurus”结尾。 该代码示例还定义了一个名为 EndsWithSaurus
的搜索谓词方法,该方法接受字符串参数并返回一个布尔值,该值指示输入字符串是否以“saurus”结尾。
Find 泛型方法从头遍历数组,将每个元素依次传递给 EndsWithSaurus
方法。 当 EndsWithSaurus
方法返回元素“Amargasaurus”的 true
时,搜索将停止。
注意
在 C# 和 Visual Basic 中,无需显式创建Predicate<string>
委托(在 Visual Basic 中Predicate(Of String)
)。 这些语言从上下文推断出正确的委托,并自动创建它。
FindLast 泛型方法用于从末尾向后搜索数组。 它查找位置 5 处的元素“Dilophosaurus”。 FindAll 泛型方法用于返回包含以“saurus”结尾的所有元素的数组。 将显示元素。
该代码示例还演示了 Exists 和 TrueForAll 泛型方法。
using namespace System;
public ref class DinoDiscoverySet
{
public:
static void Main()
{
array<String^>^ dinosaurs =
{
"Compsognathus", "Amargasaurus", "Oviraptor",
"Velociraptor", "Deinonychus", "Dilophosaurus",
"Gallimimus", "Triceratops"
};
DinoDiscoverySet^ GoMesozoic = gcnew DinoDiscoverySet(dinosaurs);
GoMesozoic->DiscoverAll();
GoMesozoic->DiscoverByEnding("saurus");
}
DinoDiscoverySet(array<String^>^ items)
{
dinosaurs = items;
}
void DiscoverAll()
{
Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}
}
void DiscoverByEnding(String^ Ending)
{
Predicate<String^>^ dinoType;
if (Ending->ToLower() == "raptor")
{
dinoType =
gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithRaptor);
}
else if (Ending->ToLower() == "tops")
{
dinoType =
gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithTops);
}
else if (Ending->ToLower() == "saurus")
{
dinoType =
gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithSaurus);
}
else
{
dinoType =
gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithSaurus);
}
Console::WriteLine(
"\nArray::Exists(dinosaurs, \"{0}\"): {1}",
Ending,
Array::Exists(dinosaurs, dinoType));
Console::WriteLine(
"\nArray::TrueForAll(dinosaurs, \"{0}\"): {1}",
Ending,
Array::TrueForAll(dinosaurs, dinoType));
Console::WriteLine(
"\nArray::Find(dinosaurs, \"{0}\"): {1}",
Ending,
Array::Find(dinosaurs, dinoType));
Console::WriteLine(
"\nArray::FindLast(dinosaurs, \"{0}\"): {1}",
Ending,
Array::FindLast(dinosaurs, dinoType));
Console::WriteLine(
"\nArray::FindAll(dinosaurs, \"{0}\"):", Ending);
array<String^>^ subArray =
Array::FindAll(dinosaurs, dinoType);
for each(String^ dinosaur in subArray)
{
Console::WriteLine(dinosaur);
}
}
private:
array<String^>^ dinosaurs;
// Search predicate returns true if a string ends in "saurus".
static bool EndsWithSaurus(String^ s)
{
if ((s->Length > 5) &&
(s->Substring(s->Length - 6)->ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
// Search predicate returns true if a string ends in "raptor".
static bool EndsWithRaptor(String^ s)
{
if ((s->Length > 5) &&
(s->Substring(s->Length - 6)->ToLower() == "raptor"))
{
return true;
}
else
{
return false;
}
}
// Search predicate returns true if a string ends in "tops".
static bool EndsWithTops(String^ s)
{
if ((s->Length > 3) &&
(s->Substring(s->Length - 4)->ToLower() == "tops"))
{
return true;
}
else
{
return false;
}
}
};
int main()
{
DinoDiscoverySet::Main();
}
/* This code example produces the following output:
Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops
Array.Exists(dinosaurs, "saurus"): True
Array.TrueForAll(dinosaurs, "saurus"): False
Array.Find(dinosaurs, "saurus"): Amargasaurus
Array.FindLast(dinosaurs, "saurus"): Dilophosaurus
Array.FindAll(dinosaurs, "saurus"):
Amargasaurus
Dilophosaurus
*/
using System;
public class DinoDiscoverySet
{
public static void Main()
{
string[] dinosaurs =
{
"Compsognathus", "Amargasaurus", "Oviraptor",
"Velociraptor", "Deinonychus", "Dilophosaurus",
"Gallimimus", "Triceratops"
};
DinoDiscoverySet GoMesozoic = new DinoDiscoverySet(dinosaurs);
GoMesozoic.DiscoverAll();
GoMesozoic.DiscoverByEnding("saurus");
}
private string[] dinosaurs;
public DinoDiscoverySet(string[] items)
{
dinosaurs = items;
}
public void DiscoverAll()
{
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
}
public void DiscoverByEnding(string Ending)
{
Predicate<string> dinoType;
switch (Ending.ToLower())
{
case "raptor":
dinoType = EndsWithRaptor;
break;
case "tops":
dinoType = EndsWithTops;
break;
case "saurus":
default:
dinoType = EndsWithSaurus;
break;
}
Console.WriteLine(
"\nArray.Exists(dinosaurs, \"{0}\"): {1}",
Ending,
Array.Exists(dinosaurs, dinoType));
Console.WriteLine(
"\nArray.TrueForAll(dinosaurs, \"{0}\"): {1}",
Ending,
Array.TrueForAll(dinosaurs, dinoType));
Console.WriteLine(
"\nArray.Find(dinosaurs, \"{0}\"): {1}",
Ending,
Array.Find(dinosaurs, dinoType));
Console.WriteLine(
"\nArray.FindLast(dinosaurs, \"{0}\"): {1}",
Ending,
Array.FindLast(dinosaurs, dinoType));
Console.WriteLine(
"\nArray.FindAll(dinosaurs, \"{0}\"):", Ending);
string[] subArray =
Array.FindAll(dinosaurs, dinoType);
foreach(string dinosaur in subArray)
{
Console.WriteLine(dinosaur);
}
}
// Search predicate returns true if a string ends in "saurus".
private bool EndsWithSaurus(string s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
// Search predicate returns true if a string ends in "raptor".
private bool EndsWithRaptor(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "raptor"))
{
return true;
}
else
{
return false;
}
}
// Search predicate returns true if a string ends in "tops".
private bool EndsWithTops(String s)
{
if ((s.Length > 3) &&
(s.Substring(s.Length - 4).ToLower() == "tops"))
{
return true;
}
else
{
return false;
}
}
}
/* This code example produces the following output:
Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops
Array.Exists(dinosaurs, "saurus"): True
Array.TrueForAll(dinosaurs, "saurus"): False
Array.Find(dinosaurs, "saurus"): Amargasaurus
Array.FindLast(dinosaurs, "saurus"): Dilophosaurus
Array.FindAll(dinosaurs, "saurus"):
Amargasaurus
Dilophosaurus
*/
open System
// Search predicate returns true if a string ends in "saurus".
let endsWithSaurus (s: string) =
s.Length > 5 && s.Substring(s.Length - 6).ToLower() = "saurus"
// Search predicate returns true if a string ends in "raptor".
let endsWithRaptor (s: string) =
s.Length > 5 && s.Substring(s.Length - 6).ToLower() = "raptor"
// Search predicate returns true if a string ends in "tops".
let endsWithTops (s: string) =
s.Length > 3 && s.Substring(s.Length - 4).ToLower() = "tops"
type DinoDiscoverySet =
{ Dinosaurs: string [] }
member this.DiscoverAll() =
printfn ""
for dino in this.Dinosaurs do
printfn $"{dino}"
member this.DiscoverByEnding(ending: string) =
let dinoType =
match ending.ToLower() with
| "raptor" -> endsWithRaptor
| "tops" -> endsWithTops
| "saurus" | _ -> endsWithSaurus
Array.Exists(this.Dinosaurs, dinoType)
|> printfn "\nArray.Exists(dinosaurs, \"%s\"): %b" ending
Array.TrueForAll(this.Dinosaurs, dinoType)
|> printfn "\nArray.TrueForAll(dinosaurs, \"%s\"): %b" ending
Array.Find(this.Dinosaurs, dinoType)
|> printfn "\nArray.Find(dinosaurs, \"%s\"): %s" ending
Array.FindLast(this.Dinosaurs, dinoType)
|> printfn "\nArray.FindLast(dinosaurs, \"%s\"): %s" ending
printfn $"\nArray.FindAll(dinosaurs, \"{ending}\"):"
for dinosaur in Array.FindAll(this.Dinosaurs, dinoType) do
printfn $"{dinosaur}"
let dinosaurs =
[| "Compsognathus"; "Amargasaurus"; "Oviraptor"
"Velociraptor"; "Deinonychus"; "Dilophosaurus"
"Gallimimus"; "Triceratops" |]
let goMesozoic = { Dinosaurs = dinosaurs }
goMesozoic.DiscoverAll()
goMesozoic.DiscoverByEnding "saurus"
// This code example produces the following output:
// Compsognathus
// Amargasaurus
// Oviraptor
// Velociraptor
// Deinonychus
// Dilophosaurus
// Gallimimus
// Triceratops
//
// Array.Exists(dinosaurs, "saurus"): true
//
// Array.TrueForAll(dinosaurs, "saurus"): false
//
// Array.Find(dinosaurs, "saurus"): Amargasaurus
//
// Array.FindLast(dinosaurs, "saurus"): Dilophosaurus
//
// Array.FindAll(dinosaurs, "saurus"):
// Amargasaurus
// Dilophosaurus
Public Class DinoDiscoverySet
Public Shared Sub Main()
Dim dinosaurs() As String = { "Compsognathus", _
"Amargasaurus", "Oviraptor", "Velociraptor", _
"Deinonychus", "Dilophosaurus", "Gallimimus", _
"Triceratops" }
Dim GoMesozoic As New DinoDiscoverySet(dinosaurs)
GoMesozoic.DiscoverAll()
GoMesozoic.DiscoverByEnding("saurus")
End Sub
Private dinosaurs As String()
Public Sub New(items() As String)
dinosaurs = items
End Sub
Public Sub DiscoverAll()
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next dinosaur
End Sub
Public Sub DiscoverByEnding(Ending As String)
Dim dinoType As Predicate(Of String)
Select Case Ending.ToLower()
Case "raptor"
dinoType = AddressOf EndsWithRaptor
Case "tops"
dinoType = AddressOf EndsWithTops
Case "saurus"
dinoType = AddressOf EndsWithSaurus
Case Else
dinoType = AddressOf EndsWithSaurus
End Select
Console.WriteLine(Environment.NewLine + _
"Array.Exists(dinosaurs, ""{0}""): {1}", _
Ending, _
Array.Exists(dinosaurs, dinoType))
Console.WriteLine(Environment.NewLine + _
"Array.TrueForAll(dinosaurs, ""{0}""): {1}", _
Ending, _
Array.TrueForAll(dinosaurs, dinoType))
Console.WriteLine(Environment.NewLine + _
"Array.Find(dinosaurs, ""{0}""): {1}", _
Ending, _
Array.Find(dinosaurs, dinoType))
Console.WriteLine(Environment.NewLine + _
"Array.FindLast(dinosaurs, ""{0}""): {1}", _
Ending, _
Array.FindLast(dinosaurs, dinoType))
Console.WriteLine(Environment.NewLine + _
"Array.FindAll(dinosaurs, ""{0}""):", Ending)
Dim subArray() As String = _
Array.FindAll(dinosaurs, dinoType)
For Each dinosaur As String In subArray
Console.WriteLine(dinosaur)
Next dinosaur
End Sub
' Search predicate returns true if a string ends in "saurus".
Private Function EndsWithSaurus(s As String) As Boolean
' AndAlso prevents evaluation of the second Boolean
' expression if the string is so short that an error
' would occur.
If (s.Length > 5) AndAlso _
(s.ToLower().EndsWith("saurus")) Then
Return True
Else
Return False
End If
End Function
' Search predicate returns true if a string ends in "raptor".
Private Function EndsWithRaptor(s As String) As Boolean
' AndAlso prevents evaluation of the second Boolean
' expression if the string is so short that an error
' would occur.
If (s.Length > 5) AndAlso _
(s.ToLower().EndsWith("raptor")) Then
Return True
Else
Return False
End If
End Function
' Search predicate returns true if a string ends in "tops".
Private Function EndsWithTops(s As String) As Boolean
' AndAlso prevents evaluation of the second Boolean
' expression if the string is so short that an error
' would occur.
If (s.Length > 3) AndAlso _
(s.ToLower().EndsWith("tops")) Then
Return True
Else
Return False
End If
End Function
End Class
' This code example produces the following output:
'
' Compsognathus
' Amargasaurus
' Oviraptor
' Velociraptor
' Deinonychus
' Dilophosaurus
' Gallimimus
' Triceratops
'
' Array.Exists(dinosaurs, "saurus"): True
'
' Array.TrueForAll(dinosaurs, "saurus"): False
'
' Array.Find(dinosaurs, "saurus"): Amargasaurus
'
' Array.FindLast(dinosaurs, "saurus"): Dilophosaurus
'
' Array.FindAll(dinosaurs, "saurus"):
' Amargasaurus
' Dilophosaurus
注解
Predicate<T> 是方法的委托,如果传递给该方法的对象与委托中定义的条件匹配,则返回 true
。
array
的元素分别传递给 Predicate<T>,在 Array中向后移动,从最后一个元素开始,以第一个元素结尾。 找到匹配项时,将停止处理。
此方法是 O(n
) 操作,其中 n
是 array
的 Length。