Array.Find<T>(T[], Predicate<T>) Méthode

Définition

Recherche un élément qui correspond aux conditions définies par le prédicat spécifié et retourne la première occurrence dans le Array entier.

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

Paramètres de type

T

Type des éléments du tableau.

Paramètres

array
T[]

Le tableau de base zéro unidimensionnel à explorer.

match
Predicate<T>

Le prédicat qui définit les conditions de l'élément à rechercher.

Retours

T

Premier élément qui correspond aux conditions définies par le prédicat spécifié, s’il est trouvé ; sinon, valeur par défaut du type T.

Exceptions

array a la valeur null.

-ou-

match a la valeur null.

Exemples

L’exemple suivant utilise un Predicate<T> délégué avec la Find méthode générique pour rechercher un tableau de Point structures. La méthode que représente le délégué, ProductGT10retourne true si le produit des champs X et Y est supérieur à 100 000. La Find méthode appelle le délégué pour chaque élément du tableau, en retournant le premier point qui répond à la condition de test.

Notes

Visual Basic, C#et F# n’ont pas besoin de créer le délégué explicitement ou de spécifier l’argument de type de la méthode générique. Les compilateurs déterminent les types nécessaires à partir des arguments de méthode que vous fournissez.

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

Au lieu de définir explicitement une méthode avec la signature nécessaire, l’instanciation d’un Predicate<T> délégué et le passage du délégué à la Find méthode, il est habituel d’utiliser une expression lambda. L’exemple suivant est identique à celui précédent, sauf qu’il utilise une expression lambda comme 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

Remarques

Il Predicate<T> s’agit d’un délégué à une méthode ou d’une expression lambda qui retourne true si l’objet passé à celui-ci correspond aux conditions définies dans le délégué ou l’expression lambda. Les éléments d’un array élément sont transmis individuellement au Predicate<T>, commençant par le premier élément et se terminant par le dernier élément. Le traitement est arrêté lorsqu’une correspondance est trouvée.

Cette méthode est une opération O(n), où n est le Length .array

Dans F#, la fonction Array.find peut être utilisée à la place.

S’applique à

Voir aussi