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

Definition

Sucht nach einem Element, das die durch das angegebene Prädikat definierten Bedingungen erfüllt, und gibt das erste Vorkommen im gesamten Array zurü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 zu durchsuchende eindimensionale und nullbasierte Array.

match
Predicate<T>

Das Prädikat, das die Bedingungen für das Element definiert, nach dem gesucht werden soll.

Gibt zurück

T

Das erste Element, das die durch das angegebene Prädikat definierten Bedingungen erfüllt, sofern vorhanden, andernfalls der Standardwert für den Typ T.

Ausnahmen

array ist null.

- oder -

match ist null.

Beispiele

Im folgenden Beispiel wird ein Predicate<T> Delegaten mit der Find generischen Methode verwendet, um ein Array von Point Strukturen zu durchsuchen. Die Methode, die der Delegat darstellt, gibt zurücktrue, ProductGT10wenn 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.

Hinweis

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 explizit eine Methode mit der erforderlichen Signatur zu definieren, einen Predicate<T> Delegaten zu instanziieren und den Delegaten an die Find -Methode zu übergeben, ist es üblich, einen Lambdaausdruck zu verwenden. Das folgende Beispiel ist identisch mit dem vorherigen, mit dem Unterschied, dass es einen Lambdaausdruck als match Argument verwendet.

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

Ist Predicate<T> ein Delegat für eine Methode oder einen Lambdaausdruck, der zurückgibt true , wenn das an ihn übergebene Objekt den im Delegaten- oder Lambdaausdruck definierten Bedingungen entspricht. Die Elemente von array werden einzeln an übergeben Predicate<T>, beginnend mit dem ersten Element und endend mit dem letzten Element. Die Verarbeitung wird beendet, wenn eine Übereinstimmung gefunden wird.

Diese Methode ist ein O(n)-Vorgang, wobei n der Length von arrayist.

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

Gilt für:

Weitere Informationen