Array.FindLast<T>(T[], Predicate<T>) Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Belirtilen koşul tarafından tanımlanan koşullarla eşleşen bir öğeyi arar ve içindeki Arrayson oluşumu döndürür.
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ür Parametreleri
- T
Dizinin öğelerinin türü.
Parametreler
- array
- T[]
Aranacak tek boyutlu, sıfır tabanlı Array .
- match
- Predicate<T>
Predicate<T> Aranacak öğenin koşullarını tanımlayan.
Döndürülenler
Bulunursa, belirtilen koşul tarafından tanımlanan koşullarla eşleşen son öğe; aksi takdirde, türü Tiçin varsayılan değerdir.
Özel durumlar
Örnekler
Aşağıdaki kod örneği , Findve FindLast genel yöntemlerini gösterirFindAll. İkisi (konum 1 ve 5'te) "saurus" ile biten 8 dinozor adını içeren bir dizi dize oluşturulur. Kod örneği, bir dize parametresi kabul eden ve giriş dizesinin "saurus" ile bitip bitmediğini belirten bir Boole değeri döndüren adlı EndsWithSaurusbir arama koşulu yöntemini de tanımlar.
Genel Find yöntem, diziyi baştan geçirerek her öğeyi yönteme EndsWithSaurus geçirir. Yöntem "Amargasaurus" öğesini döndürdüğünde EndsWithSaurustrue arama durdurulur.
Note
C# ve Visual Basic'daPredicate<string> temsilcisinin (Visual Basic'da Predicate(Of String)) açıkça oluşturulması gerekmez. Bu diller, bağlamdan doğru temsilciyi çıkarsar ve otomatik olarak oluşturur.
Genel FindLast yöntem, diziyi sonundan geriye doğru aramak için kullanılır. 5 konumunda "Dilophosaurus" öğesini bulur. Genel FindAll yöntem, "eş anlamlılar" ile biten tüm öğeleri içeren bir dizi döndürmek için kullanılır. Öğeler görüntülenir.
Kod örneği ve genel yöntemlerini de gösterir ExistsTrueForAll .
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
Açıklamalar
, Predicate<T> geçirilen nesne temsilcide tanımlanan koşullarla eşleşiyorsa döndüren true bir yöntemin temsilcisidir. öğeleriarray, son öğeden başlayıp ilk öğeyle biten öğesinde Predicate<T>geriye doğru hareket ederek öğesine tek tek geçirilirArray. Eşleşme bulunduğunda işlem durdurulur.
Bu yöntem bir O(n) işlemidir; burada n değeridir Lengtharray.