Array.Find<T>(T[], Predicate<T>) Metoda

Definicja

Wyszukuje element zgodny z warunkami zdefiniowanymi przez określony predykat i zwraca pierwsze wystąpienie w całym Arrayobiekcie .

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

Parametry typu

T

Typ elementów tablicy.

Parametry

array
T[]

Jednowymiarowa, zero-oparta tablica do wyszukiwania.

match
Predicate<T>

Predykat definiujący warunki elementu do wyszukania.

Zwraca

T

Pierwszy element, który pasuje do warunków zdefiniowanych przez określony predykat, jeśli zostanie znaleziony; w przeciwnym razie wartość domyślna dla typu T.

Wyjątki

array to null.

-lub-

match to null.

Przykłady

W poniższym przykładzie użyto delegata Predicate<T> z Find metodą ogólną, aby wyszukać tablicę Point struktur. Metoda reprezentowana przez delegata zwraca wartość , ProductGT10jeśli true produkt pól X i Y jest większy niż 100 000. Metoda Find wywołuje delegata dla każdego elementu tablicy, zwracając pierwszy punkt spełniający warunek testu.

Uwaga

Użytkownicy języka Visual Basic, C# i F# nie muszą jawnie tworzyć delegata ani określać argumentu typu metody ogólnej. Kompilatory określają niezbędne typy z argumentów metody, które podajesz.

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

Zamiast jawnie definiować metodę z wymaganym podpisem Predicate<T> , utworzyć wystąpienie delegata i przekazać delegata do Find metody, jest ona niestandardowa do używania wyrażenia lambda. Poniższy przykład jest identyczny z poprzednim, z wyjątkiem tego, że używa wyrażenia lambda jako argumentu match .

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

Uwagi

Jest Predicate<T> delegatem do metody lub wyrażenia lambda, które zwraca true , jeśli obiekt przekazany do niego pasuje do warunków zdefiniowanych w wyrażeniu delegata lub lambda. Elementy elementu array są indywidualnie przekazywane do Predicate<T>elementu , począwszy od pierwszego elementu i kończące się ostatnim elementem. Przetwarzanie jest zatrzymywane po znalezieniu dopasowania.

Ta metoda jest operacją O(), gdzie n jest wartością Lengtharray.n

W języku F# można zamiast tego użyć funkcji Array.find .

Dotyczy

Zobacz też