Array.Find<T>(T[], Predicate<T>) メソッド

定義

指定された述語によって定義された条件と一致する要素を検索し、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

型パラメーター

T

配列要素の型。

パラメーター

array
T[]

検索する 1 次元の配列。インデックス番号が 0 から始まる必要があります。

match
Predicate<T>

検索する要素の条件を定義する述語。

戻り値

T

見つかった場合は、指定された述語によって定義された条件と一致する最初の要素。それ以外の場合は、型 T の既定値。

例外

arraynullです。

または

matchnullです。

次の例では、デリゲートを Predicate<T> ジェネリック メソッドと共に Find 使用して、構造体の Point 配列を検索します。 デリゲートが表すメソッドは、 ProductGT10X フィールドと Y フィールドの積が 100,000 を超える場合に を返 true します。 メソッドは Find 、配列の各要素のデリゲートを呼び出し、テスト条件を満たす最初のポイントを返します。

注意

Visual Basic、C#、F# のユーザーは、デリゲートを明示的に作成したり、ジェネリック メソッドの型引数を指定したりする必要はありません。 コンパイラは、指定したメソッド引数から必要な型を決定します。

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

必要なシグネチャを使用してメソッドを明示的に定義し、デリゲートを Predicate<T> インスタンス化し、デリゲートをメソッドに Find 渡すのではなく、ラムダ式を使用するのが慣例です。 次の例は、ラムダ式を引数として使用する点を除き、前の例と match 同じです。

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

注釈

Predicate<T>は、渡されたオブジェクトがデリゲートまたはラムダ式で定義されている条件と一致する場合に返trueされるメソッドまたはラムダ式へのデリゲートです。 の array 要素は、 に個別に渡されます Predicate<T>。最初の要素から始まり、最後の要素で終わる。 一致が見つかると、処理が停止します。

このメソッドは O(n) 操作です。ここで nLength は の arrayです。

F# では、 代わりに Array.find 関数を使用できます。

適用対象

こちらもご覧ください