Array.Find<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 ilk oluşumu döndürür Array.
public:
generic <typename T>
static T Find(cli::array <T> ^ array, Predicate<T> ^ match);
public static T Find<T> (T[] array, Predicate<T> match);
public static T? Find<T> (T[] array, Predicate<T> match);
static member Find : 'T[] * Predicate<'T> -> 'T
Public Shared Function Find(Of T) (array As T(), match As Predicate(Of T)) As T
Tür Parametreleri
- T
Dizinin öğelerini türü.
Parametreler
- array
- T[]
Aranacak tek boyutlu, sıfır tabanlı dizi.
- match
- Predicate<T>
Aranacak öğenin koşullarını tanımlayan koşul.
Döndürülenler
- T
Bulunursa, belirtilen koşul tarafından tanımlanan koşullarla eşleşen ilk öğe; aksi takdirde, türü T
için varsayılan değerdir.
Özel durumlar
Örnekler
Aşağıdaki örnek, bir Predicate<T> yapı dizisini Find Point aramak için genel yöntemiyle bir temsilci kullanır. Temsilcinin temsil ettiği yöntemi, ProductGT10
X ve Y alanlarının ürünü 100.000'den büyükse döndürür true
. Find yöntemi, dizinin her öğesi için temsilciyi çağırır ve test koşulunu karşılayan ilk noktayı döndürür.
Not
Visual Basic, C# ve F# kullanıcılarının temsilciyi açıkça oluşturması veya genel yöntemin tür bağımsız değişkenini belirtmesi gerekmez. Derleyiciler, sağladığınız yöntem bağımsız değişkenlerinden gerekli türleri belirler.
using System;
using System.Drawing;
public class Example
{
public static void Main()
{
// Create an array of five Point structures.
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };
// Find the first Point structure for which X times Y
// is greater than 100000.
Point first = Array.Find(points, ProductGT10);
// Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
}
// Return true if X times Y is greater than 100000.
private static bool ProductGT10(Point p)
{
return p.X * p.Y > 100000;
}
}
// The example displays the following output:
// Found: X = 275, Y = 395
open System
open System.Drawing
// Return true if X times Y is greater than 100000.
let productGT10 (p: Point) = p.X * p.Y > 100000
// Create an array of five Point structures.
let points =
[| Point(100, 200)
Point(150, 250)
Point(250, 375)
Point(275, 395)
Point(295, 450) |]
// Find the first Point structure for which X times Y
// is greater than 100000.
let first = Array.Find(points, productGT10)
// let first = Array.find productGT10 points
// Display the first structure found.
printfn $"Found: X = {first.X}, Y = {first.Y}"
// The example displays the following output:
// Found: X = 275, Y = 395
Imports System.Drawing
Public Module Example
Public Sub Main()
' Create an array of five Point structures.
Dim points() As Point = { new Point(100, 200), _
new Point(150, 250), new Point(250, 375), _
new Point(275, 395), new Point(295, 450) }
' Find the first Point structure for which X times Y
' is greater than 100000.
Dim first As Point = Array.Find(points, AddressOf ProductGT10)
' Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", _
first.X, first.Y)
End Sub
' Return true if X times Y is greater than 100000.
Private Function ProductGT10(ByVal p As Point) As Boolean
Return p.X * p.Y > 100000
End Function
End Module
' The example displays the following output:
' Found: X = 275, Y = 395
Gerekli imzaya sahip bir yöntemi açıkça tanımlamak, temsilci Predicate<T> örneği oluşturmak ve temsilciyi yönteme Find geçirmek yerine, lambda ifadesi kullanmak özeldir. Aşağıdaki örnek, bağımsız değişken olarak lambda ifadesi kullanması dışında öncekiyle match
aynıdır.
using System;
using System.Drawing;
public class Example
{
public static void Main()
{
// Create an array of five Point structures.
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };
// Find the first Point structure for which X times Y
// is greater than 100000.
Point first = Array.Find(points, p => p.X * p.Y > 100000);
// Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
}
}
// The example displays the following output:
// Found: X = 275, Y = 395
open System
open System.Drawing
let points =
[| Point(100, 200)
Point(150, 250)
Point(250, 375)
Point(275, 395)
Point(295, 450) |]
// Find the first Point structure for which X times Y
// is greater than 100000.
let first = Array.Find(points, fun p -> p.X * p.Y > 100000)
// let first = points |> Array.find (fun p -> p.X * p.Y > 100000)
// Display the first structure found.
printfn $"Found: X = {first.X}, Y = {first.Y}"
// The example displays the following output:
// Found: X = 275, Y = 395
Imports System.Drawing
Public Module Example
Public Sub Main()
' Create an array of five Point structures.
Dim points() As Point = { new Point(100, 200), _
new Point(150, 250), new Point(250, 375), _
new Point(275, 395), new Point(295, 450) }
' Find the first Point structure for which X times Y
' is greater than 100000.
Dim first As Point = Array.Find(points,
Function(p) p.X * p.Y > 100000)
' Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", _
first.X, first.Y)
End Sub
End Module
' The example displays the following output:
' Found: X = 275, Y = 395
Açıklamalar
, Predicate<T> bir yönteme veya nesneye geçirilen nesnenin temsilci veya lambda ifadesinde tanımlanan koşullarla eşleşip eşleşmediğini döndüren true
bir lambda ifadesi temsilcisidir. öğeleri array
, ilk öğeden başlayıp son öğeyle biten öğesine tek tek geçirilir Predicate<T>. Eşleşme bulunduğunda işlem durdurulur.
Bu yöntem bir O(n
) işlemidir ve burada n
değeridir Length array
.
F# dilinde bunun yerine Array.find işlevi kullanılabilir.