共用方式為


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[]

要搜尋的一維、以零起始的陣列。

match
Predicate<T>

定義要搜尋之項目條件的述詞。

傳回

T

如果找到,符合指定述詞所定義之條件的第一個專案;否則,類型 T的預設值。

例外狀況

array null

-或-

match null

範例

下列範例會使用具有 Find 泛型方法的 Predicate<T> 委派來搜尋 Point 結構的陣列。 如果 X 和 Y 欄位的乘積大於 100,000,則委派所代表的方法 ProductGT10傳回 trueFind 方法會針對數位的每個元素呼叫委派,並傳回符合測試條件的第一個點。

注意

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 方法,而是習慣使用 Lambda 表達式。 下列範例與上一個範例相同,不同之處在於它會使用 Lambda 表達式作為 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> 是方法或 Lambda 表達式的委派,如果傳遞至方法或 Lambda 運算式符合委派或 Lambda 運算式中定義的條件,則會傳回 truearray 的專案會個別傳遞至 Predicate<T>,從第一個專案開始,最後一個項目結束。 找到相符專案時,就會停止處理。

這個方法是 O(n) 作業,其中 narrayLength

在 F# 中,可以改用 Array.find 函式。

適用於

另請參閱