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 的默认值。

例外

arraynull

- 或 -

matchnull

示例

以下示例使用 Predicate<T> 具有 泛型方法的 Find 委托来搜索结构的数组 Point 。 如果 X 和 Y 字段的乘积大于 100,000,则委托表示 ProductGT10的方法 返回 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

通常使用 lambda 表达式,而不是使用必要的签名显式定义方法、实例化 Predicate<T> 委托并将委托传递给 Find 方法。 下面的示例与上一个示例相同,只不过它使用 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 表达式的true委托,如果传递给该方法的对象与委托或 lambda 表达式中定义的条件匹配,则返回 。 的 array 元素分别传递到 Predicate<T>,从第一个元素开始,以最后一个元素结束。 找到匹配项时,将停止处理。

此方法是 O (n) 操作,其中 nLengtharray

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

适用于

另请参阅