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 형식의 기본값입니다.

예외

array이(가) null인 경우

또는

match이(가) null인 경우

예제

다음 예제에서는 제네릭 메서드와 대리자를 Find 사용하여 Predicate<T> 구조체 배열 Point 을 검색합니다. 대리자가 나타내는 ProductGT10메서드는 X 및 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) 연산이며 여기서 n 는 .의 arrayLength 연산입니다.

F#에서는 Array.find 함수를 대신 사용할 수 있습니다.

적용 대상

추가 정보