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

Definition

Söker efter ett element som matchar de villkor som definieras av det angivna predikatet och returnerar den första förekomsten i hela 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

Typparametrar

T

Typ av element i matrisen.

Parametrar

array
T[]

Den endimensionella, nollbaserade matrisen som ska sökas.

match
Predicate<T>

Predikatet som definierar villkoren för elementet som ska sökas efter.

Returer

T

Det första elementet som matchar de villkor som definierats av det angivna predikatet, om det hittas; annars är standardvärdet för typen T.

Undantag

array är null.

-eller-

match är null.

Exempel

I följande exempel används ett Predicate<T> ombud med den Find generiska metoden för att söka i en matris med Point strukturer. Metoden som ombudet representerar, ProductGT10, returnerar true om produkten för X- och Y-fälten är större än 100 000. Metoden Find anropar ombudet för varje element i matrisen och returnerar den första punkten som uppfyller testvillkoret.

Note

Visual Basic,C#- och F#-användare behöver inte skapa ombudet explicit eller ange typargumentet för den generiska metoden. Kompilatorerna avgör vilka typer som behövs från de metodargument som du anger.

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

I stället för att explicit definiera en metod med den nödvändiga signaturen, instansiera ett Predicate<T> ombud och skicka ombudet Find till metoden är det vanligt att använda ett lambda-uttryck. Följande exempel är identiskt med det föregående, förutom att det använder ett lambda-uttryck som match argument.

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

Kommentarer

Predicate<T> är ett ombud till en metod eller ett lambda-uttryck som returnerar true om objektet som skickas till det matchar de villkor som definierats i delegat- eller lambda-uttrycket. Elementen array i skickas individuellt till Predicate<T>, från och med det första elementet och slutar med det sista elementet. Bearbetningen stoppas när en matchning hittas.

Den här metoden är en O()-nåtgärd, där n är för Lengtharray.

I F#kan funktionen Array.find användas i stället.

Gäller för

Se även