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

Definice

Vyhledá prvek, který odpovídá podmínkám definovaným zadaným predikátem, a vrátí první výskyt v rámci celého Arraysouboru .

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 prvků pole.

Parametry

array
T[]

Jednorozměrné pole založené na nule, které chcete hledat.

match
Predicate<T>

Predikát, který definuje podmínky prvku, který se má vyhledat.

Návraty

T

První prvek, který odpovídá podmínkám definovaným zadaným predikátem, pokud byl nalezen; jinak výchozí hodnota pro typ T.

Výjimky

array je null.

-nebo-

match je null.

Příklady

Následující příklad používá delegáta Predicate<T> s obecnou metodou Find k vyhledávání pole Point struktur. Metoda delegáta představuje , vrátítrue, ProductGT10pokud je součin polí X a Y větší než 100 000. Find Metoda volá delegáta pro každý prvek pole, který vrací první bod, který splňuje testovací podmínku.

Poznámka

Visual Basic, C# a F# uživatelé nemusí delegáta explicitně vytvářet nebo zadávat typ argumentu obecné metody. Kompilátory určují potřebné typy z argumentů metody, které zadáte.

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

Místo explicitního definování metody s potřebným podpisem, vytvoření Predicate<T> instance delegáta a předání delegáta metodě Find je vlastní použít výraz lambda. Následující příklad je stejný jako předchozí, s tím rozdílem, že jako argument používá výraz match lambda.

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

Poznámky

Je Predicate<T> delegátem metody nebo výrazu lambda, který vrátí true , pokud objekt předaný odpovídá podmínkám definovaným ve výrazu delegáta nebo lambda. Prvky array jsou jednotlivě předány Predicate<T>do , počínaje prvním prvkem a končí posledním prvkem. Zpracování se zastaví, když se najde shoda.

Tato metoda je operace O(n), kde n je Length .array

V jazyce F# lze místo toho použít funkci Array.find .

Platí pro

Viz také