List<T>.FindIndex 方法

定义

搜索与指定谓词所定义的条件相匹配的元素,返回 List<T> 或它的一部分中第一个匹配项的从零开始的索引。 如果未找到与条件匹配的项,则此方法返回 -1。

重载

FindIndex(Int32, Int32, Predicate<T>)

搜索与指定谓词所定义的条件相匹配的一个元素,并返回 List<T> 中从指定的索引开始、包含指定元素个数的元素范围内第一个匹配项的从零开始的索引。

FindIndex(Predicate<T>)

搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中第一个匹配元素的从零开始的索引。

FindIndex(Int32, Predicate<T>)

搜索与指定谓词所定义的条件相匹配的元素,并返回 List<T> 中从指定索引到最后一个元素的元素范围内第一个匹配项的从零开始的索引。

FindIndex(Int32, Int32, Predicate<T>)

Source:
List.cs
Source:
List.cs
Source:
List.cs

搜索与指定谓词所定义的条件相匹配的一个元素,并返回 List<T> 中从指定的索引开始、包含指定元素个数的元素范围内第一个匹配项的从零开始的索引。

public:
 int FindIndex(int startIndex, int count, Predicate<T> ^ match);
public int FindIndex (int startIndex, int count, Predicate<T> match);
member this.FindIndex : int * int * Predicate<'T> -> int
Public Function FindIndex (startIndex As Integer, count As Integer, match As Predicate(Of T)) As Integer

参数

startIndex
Int32

从零开始的搜索的起始索引。

count
Int32

要搜索的部分中的元素数。

match
Predicate<T>

Predicate<T> 委托,用于定义要搜索的元素的条件。

返回

如果找到与 match 定义的条件相匹配的第一个元素,则为该元素的从零开始的索引;否则为 -1。

例外

matchnull

startIndex 超出了 List<T> 的有效索引范围。

- 或 -

count 小于 0。

- 或 -

startIndexcount 未在 List<T> 中指定有效部分。

示例

以下示例定义了一个具有两个 Employee 字段 的类: NameId。 它还使用 EmployeeSearch 单个方法 定义类, StartsWith该方法指示字段是否 Employee.Name 以提供给 EmployeeSearch 类构造函数的指定子字符串开头。 记下此方法的签名

public bool StartsWith(Employee e)
Public Function StartsWith(e As Employee) As Boolean

对应于可传递给 方法的委托的 FindIndex 签名。 该示例实例化一个 List<Employee> 对象,向该对象添加一些 Employee 对象,然后调用 FindIndex(Int32, Int32, Predicate<T>) 方法两次以搜索整个集合 (即从索引 0 到索引 Count 1) 的成员。 第一次,它搜索其字段以“J”开头的第一个Employee对象Name;第二次,它搜索其Name字段以“Ju”开头的第一个Employee对象。

using System;
using System.Collections.Generic;

public class Employee : IComparable
{
   public String Name { get; set; }
   public int Id { get; set; }

   public int CompareTo(Object o )
   {
      Employee e = o as Employee;
      if (e == null)
         throw new ArgumentException("o is not an Employee object.");

      return Name.CompareTo(e.Name);
   }
}

public class EmployeeSearch
{
   String _s;

   public EmployeeSearch(String s)
   {
      _s = s;
   }

   public bool StartsWith(Employee e)
   {
      return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase);
   }
}

public class Example
{
   public static void Main()
   {
      var employees = new List<Employee>();
      employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 },
                                           new Employee { Name = "Jill", Id = 3 },
                                           new Employee { Name = "Dave", Id = 5 },
                                           new Employee { Name = "Jack", Id = 8 },
                                           new Employee { Name = "Judith", Id = 12 },
                                           new Employee { Name = "Robert", Id = 14 },
                                           new Employee { Name = "Adam", Id = 1 } } );
      employees.Sort();

      var es = new EmployeeSearch("J");
      Console.WriteLine("'J' starts at index {0}",
                        employees.FindIndex(0, employees.Count - 1, es.StartsWith));

      es = new EmployeeSearch("Ju");
      Console.WriteLine("'Ju' starts at index {0}",
                        employees.FindIndex(0, employees.Count - 1,es.StartsWith));
   }
}
// The example displays the following output:
//       'J' starts at index 3
//       'Ju' starts at index 5
Imports System.Collections.Generic

Public Class Employee : Implements IComparable
   Public Property Name As String
   Public Property Id As Integer
   
   Public Function CompareTo(o As Object) As Integer _
         Implements IComparable.CompareTo
      Dim e As Employee = TryCast(o, Employee)
      If e Is Nothing Then
         Throw New ArgumentException("o is not an Employee object.")
      End If

      Return Name.CompareTo(e.Name)
   End Function
End Class

Public Class EmployeeSearch
   Dim _s As String
   
   Public Sub New(s As String)
      _s = s
   End Sub
   
   Public Function StartsWith(e As Employee) As Boolean
      Return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase)
   End Function
End Class

Module Example
   Public Sub Main()
      Dim employees As New List(Of Employee)()
      employees.AddRange( { New Employee() With { .Name = "Frank", .Id = 2 },
                            New Employee() With { .Name = "Jill", .Id = 3 },
                            New Employee() With { .Name = "Dave", .Id = 5 },
                            New Employee() With { .Name = "Jack", .Id = 8 },
                            New Employee() With { .Name = "Judith", .Id = 12 },
                            New Employee() With { .Name = "Robert", .Id = 14 },
                            New Employee() With { .Name = "Adam", .Id = 1 } } )
      employees.Sort()

      Dim es As New EmployeeSearch("J")
      Console.WriteLine("'J' starts at index {0}",
                        employees.FindIndex(0, employees.Count - 1,
                                            AddressOf es.StartsWith))
      es = New EmployeeSearch("Ju")
      Console.WriteLine("'Ju' starts at index {0}",
                        employees.FindIndex(0, employees.Count - 1,
                                            AddressOf es.StartsWith))
   End Sub
End Module
' The example displays the following output:
'       'J' starts at index 3
'       'Ju' starts at index 5

注解

List<T>如果 count 大于 0,则向前搜索从 开始startIndex,在startIndexcount减 1 处结束。

Predicate<T>是方法的true委托,如果传递给该方法的对象与委托中定义的条件匹配,则返回 。 当前 List<T> 的元素单独传递给 Predicate<T> 委托。 委托具有签名:

public bool methodName(T obj)
Public Function methodName(obj As T) As Boolean

此方法执行线性搜索;因此,此方法是 O (n) 操作,其中 ncount

另请参阅

适用于

FindIndex(Predicate<T>)

Source:
List.cs
Source:
List.cs
Source:
List.cs

搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中第一个匹配元素的从零开始的索引。

public:
 int FindIndex(Predicate<T> ^ match);
public int FindIndex (Predicate<T> match);
member this.FindIndex : Predicate<'T> -> int
Public Function FindIndex (match As Predicate(Of T)) As Integer

参数

match
Predicate<T>

Predicate<T> 委托,用于定义要搜索的元素的条件。

返回

如果找到与 match 定义的条件相匹配的第一个元素,则为该元素的从零开始的索引;否则为 -1。

例外

matchnull

示例

以下示例定义了一个具有两个 Employee 字段 的类: NameId。 它还使用 EmployeeSearch 单个方法 定义类, StartsWith该方法指示字段是否 Employee.Name 以提供给 EmployeeSearch 类构造函数的指定子字符串开头。 记下此方法的签名

public bool StartsWith(Employee e)
Public Function StartsWith(e As Employee) As Boolean

对应于可传递给 方法的委托的 FindIndex 签名。 该示例实例化一个 List<Employee> 对象,向该对象添加一些Employee对象,然后调用方法两次以搜索整个集合,第一次调用FindIndex(Int32, Int32, Predicate<T>)其字段以“J”开头的第一个Employee对象Name,第二次调用其Name字段以“Ju”开头的第一个Employee对象。

using System;
using System.Collections.Generic;

public class Employee : IComparable
{
   public String Name { get; set; }
   public int Id { get; set; }

   public int CompareTo(Object o )
   {
      Employee e = o as Employee;
      if (e == null)
         throw new ArgumentException("o is not an Employee object.");

      return Name.CompareTo(e.Name);
   }
}

public class EmployeeSearch
{
   String _s;

   public EmployeeSearch(String s)
   {
      _s = s;
   }

   public bool StartsWith(Employee e)
   {
      return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase);
   }
}

public class Example
{
   public static void Main()
   {
      var employees = new List<Employee>();
      employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 },
                                           new Employee { Name = "Jill", Id = 3 },
                                           new Employee { Name = "Dave", Id = 5 },
                                           new Employee { Name = "Jack", Id = 8 },
                                           new Employee { Name = "Judith", Id = 12 },
                                           new Employee { Name = "Robert", Id = 14 },
                                           new Employee { Name = "Adam", Id = 1 } } );
      employees.Sort();

      var es = new EmployeeSearch("J");
      Console.WriteLine("'J' starts at index {0}",
                        employees.FindIndex(es.StartsWith));

      es = new EmployeeSearch("Ju");
      Console.WriteLine("'Ju' starts at index {0}",
                        employees.FindIndex(es.StartsWith));
   }
}
// The example displays the following output:
//       'J' starts at index 3
//       'Ju' starts at index 5
Imports System.Collections.Generic

Public Class Employee : Implements IComparable
   Public Property Name As String
   Public Property Id As Integer
   
   Public Function CompareTo(o As Object) As Integer _
         Implements IComparable.CompareTo
      Dim e As Employee = TryCast(o, Employee)
      If e Is Nothing Then
         Throw New ArgumentException("o is not an Employee object.")
      End If

      Return Name.CompareTo(e.Name)
   End Function
End Class

Public Class EmployeeSearch
   Dim _s As String
   
   Public Sub New(s As String)
      _s = s
   End Sub
   
   Public Function StartsWith(e As Employee) As Boolean
      Return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase)
   End Function
End Class

Module Example
   Public Sub Main()
      Dim employees As New List(Of Employee)()
      employees.AddRange( { New Employee() With { .Name = "Frank", .Id = 2 },
                            New Employee() With { .Name = "Jill", .Id = 3 },
                            New Employee() With { .Name = "Dave", .Id = 5 },
                            New Employee() With { .Name = "Jack", .Id = 8 },
                            New Employee() With { .Name = "Judith", .Id = 12 },
                            New Employee() With { .Name = "Robert", .Id = 14 },
                            New Employee() With { .Name = "Adam", .Id = 1 } } )
      employees.Sort()

      Dim es As New EmployeeSearch("J")
      Console.WriteLine("'J' starts at index {0}",
                        employees.FindIndex(AddressOf es.StartsWith))
      es = New EmployeeSearch("Ju")
      Console.WriteLine("'Ju' starts at index {0}",
                        employees.FindIndex(AddressOf es.StartsWith))
   End Sub
End Module
' The example displays the following output:
'       'J' starts at index 3
'       'Ju' starts at index 5

注解

List<T> 第一个元素开始,到最后一个元素结束,向前搜索 。

Predicate<T>是方法的true委托,如果传递给该方法的对象与委托中定义的条件匹配,则返回 。 当前 List<T> 的元素单独传递给 Predicate<T> 委托。 委托具有签名:

public bool methodName(T obj)
Public Function methodName(obj As T) As Boolean

此方法执行线性搜索;因此,此方法是 O (n) 操作,其中 nCount

另请参阅

适用于

FindIndex(Int32, Predicate<T>)

Source:
List.cs
Source:
List.cs
Source:
List.cs

搜索与指定谓词所定义的条件相匹配的元素,并返回 List<T> 中从指定索引到最后一个元素的元素范围内第一个匹配项的从零开始的索引。

public:
 int FindIndex(int startIndex, Predicate<T> ^ match);
public int FindIndex (int startIndex, Predicate<T> match);
member this.FindIndex : int * Predicate<'T> -> int
Public Function FindIndex (startIndex As Integer, match As Predicate(Of T)) As Integer

参数

startIndex
Int32

从零开始的搜索的起始索引。

match
Predicate<T>

Predicate<T> 委托,用于定义要搜索的元素的条件。

返回

如果找到与 match 定义的条件相匹配的第一个元素,则为该元素的从零开始的索引;否则为 -1。

例外

matchnull

startIndex 超出了 List<T> 的有效索引范围。

示例

以下示例定义了一个具有两个 Employee 字段 的类: NameId。 它还使用 EmployeeSearch 单个方法 定义类, StartsWith该方法指示字段是否 Employee.Name 以提供给 EmployeeSearch 类构造函数的指定子字符串开头。 记下此方法的签名

public bool StartsWith(Employee e)
Public Function StartsWith(e As Employee) As Boolean

对应于可传递给 方法的委托的 FindIndex 签名。 该示例实例化一个 对象,向其添加一些Employee对象,然后调用 FindIndex(Int32, Int32, Predicate<T>) 方法两次以从其第五个List<Employee>成员开始搜索集合 (即位于索引 4) 的成员。 第一次,它搜索其字段以“J”开头的第一个Employee对象Name;第二次,它搜索其Name字段以“Ju”开头的第一个Employee对象。

using System;
using System.Collections.Generic;

public class Employee : IComparable
{
   public String Name { get; set; }
   public int Id { get; set; }

   public int CompareTo(Object o )
   {
      Employee e = o as Employee;
      if (e == null)
         throw new ArgumentException("o is not an Employee object.");

      return Name.CompareTo(e.Name);
   }
}

public class EmployeeSearch
{
   String _s;

   public EmployeeSearch(String s)
   {
      _s = s;
   }

   public bool StartsWith(Employee e)
   {
      return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase);
   }
}

public class Example
{
   public static void Main()
   {
      var employees = new List<Employee>();
      employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 },
                                           new Employee { Name = "Jill", Id = 3 },
                                           new Employee { Name = "Dave", Id = 5 },
                                           new Employee { Name = "Jack", Id = 8 },
                                           new Employee { Name = "Judith", Id = 12 },
                                           new Employee { Name = "Robert", Id = 14 },
                                           new Employee { Name = "Adam", Id = 1 } } );
      employees.Sort();

      var es = new EmployeeSearch("J");
      int index = employees.FindIndex(4, es.StartsWith);
      Console.WriteLine("Starting index of'J': {0}",
                        index >= 0 ? index.ToString() : "Not found");

      es = new EmployeeSearch("Ju");
      index = employees.FindIndex(4, es.StartsWith);
      Console.WriteLine("Starting index of 'Ju': {0}",
                        index >= 0 ? index.ToString() : "Not found");
   }
}
// The example displays the following output:
//       'J' starts at index 4
//       'Ju' starts at index 5
Imports System.Collections.Generic

Public Class Employee : Implements IComparable
   Public Property Name As String
   Public Property Id As Integer
   
   Public Function CompareTo(o As Object) As Integer _
         Implements IComparable.CompareTo
      Dim e As Employee = TryCast(o, Employee)
      If e Is Nothing Then
         Throw New ArgumentException("o is not an Employee object.")
      End If

      Return Name.CompareTo(e.Name)
   End Function
End Class

Public Class EmployeeSearch
   Dim _s As String
   
   Public Sub New(s As String)
      _s = s
   End Sub
   
   Public Function StartsWith(e As Employee) As Boolean
      Return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase)
   End Function
End Class

Module Example
   Public Sub Main()
      Dim employees As New List(Of Employee)()
      employees.AddRange( { New Employee() With { .Name = "Frank", .Id = 2 },
                            New Employee() With { .Name = "Jill", .Id = 3 },
                            New Employee() With { .Name = "Dave", .Id = 5 },
                            New Employee() With { .Name = "Jack", .Id = 8 },
                            New Employee() With { .Name = "Judith", .Id = 12 },
                            New Employee() With { .Name = "Robert", .Id = 14 },
                            New Employee() With { .Name = "Adam", .Id = 1 } } )
      employees.Sort()

      Dim es As New EmployeeSearch("J")
      Dim index As Integer = employees.FindIndex(4, AddressOf es.StartsWith)        
      Console.WriteLine("Starting index of'J': {0}",
                        If(index >= 0, index.ToString(), "Not found"))

      es = New EmployeeSearch("Ju")
      index = employees.FindIndex(4, AddressOf es.StartsWith) 
      Console.WriteLine("Starting index of'Ju': {0}",
                        If(index >= 0, index.ToString(), "Not found"))

   End Sub
End Module
' The example displays the following output:
'       'J' starts at index 4
'       'Ju' starts at index 5

注解

List<T>从 开始startIndex,在最后一个元素处结束,向前搜索 。

Predicate<T>是方法的true委托,如果传递给该方法的对象与委托中定义的条件匹配,则返回 。 当前 List<T> 的元素单独传递给 Predicate<T> 委托。 委托具有签名:

public bool methodName(T obj)
Public Function methodName(obj As T) As Boolean

此方法执行线性搜索;因此,此方法是 O (n) 操作,其中 n 是从 startIndex 到 末尾的 List<T>元素数。

另请参阅

适用于