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

Definition

Sucht nach einem Element, das den vom angegebenen Prädikat definierten Bedingungen entspricht, und gibt das erste Vorkommen innerhalb des gesamten ArrayZurück.

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

Typparameter

T

Der Typ der Elemente des Arrays.

Parameter

array
T[]

Das eindimensionale, nullbasierte Array, das durchsucht werden soll.

match
Predicate<T>

Das Prädikat, das die Bedingungen des elements definiert, nach dem gesucht werden soll.

Gibt zurück

T

Das erste Element, das den durch das angegebene Prädikat definierten Bedingungen entspricht, falls gefunden; andernfalls der Standardwert für den Typ T.

Ausnahmen

array ist null.

-oder-

match ist null.

Beispiele

Im folgenden Beispiel wird ein Predicate<T> Delegat mit der Find generischen Methode verwendet, um ein Array von Point Strukturen zu durchsuchen. Die Methode, die der Delegat darstellt, gibt zurückProductGT10, truewenn das Produkt der Felder X und Y größer als 100.000 ist. Die Find Methode ruft den Delegaten für jedes Element des Arrays auf und gibt den ersten Punkt zurück, der die Testbedingung erfüllt.

Note

Visual Basic-, C#- und F#-Benutzer müssen den Delegaten nicht explizit erstellen oder das Typargument der generischen Methode angeben. Die Compiler bestimmen die erforderlichen Typen aus den von Ihnen angegebenen Methodenargumenten.

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

Anstatt eine Methode mit der erforderlichen Signatur explizit zu definieren, einen Predicate<T> Delegaten zu instanziieren und den Delegaten an die Find Methode zu übergeben, ist es üblich, einen Lambda-Ausdruck zu verwenden. Das folgende Beispiel ist identisch mit dem vorherigen, mit der Ausnahme, dass ein Lambda-Ausdruck als Argument verwendet match wird.

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

Hinweise

Dies Predicate<T> ist ein Delegat an eine Methode oder einen Lambda-Ausdruck, der zurückgibt true , wenn das an sie übergebene Objekt mit den bedingungen übereinstimmt, die im Delegaten- oder Lambda-Ausdruck definiert sind. Die Elemente von array werden einzeln an das Predicate<T>erste Element übergeben, beginnend mit dem ersten Element und enden mit dem letzten Element. Die Verarbeitung wird beendet, wenn eine Übereinstimmung gefunden wird.

Bei dieser Methode handelt es sich um einen O()-Vorgang, bei dem n es sich um einen n OLength(array)-Vorgang handelt.

In F# kann stattdessen die Array.find-Funktion verwendet werden.

Gilt für:

Weitere Informationen