List<T>.FindIndex List<T>.FindIndex List<T>.FindIndex List<T>.FindIndex Method

定義

搜尋符合指定述詞所定義之條件的元素,並傳回 List<T> 內或它的一部分中第一個相符元素之以零起始的索引。Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within the List<T> or a portion of it. 如果找不到符合條件的項目,這個方法會傳回 -1。This method returns -1 if an item that matches the conditions is not found.

多載

FindIndex(Predicate<T>) FindIndex(Predicate<T>) FindIndex(Predicate<T>) FindIndex(Predicate<T>)

搜尋符合指定之述詞所定義的條件之項目,並傳回整個 List<T> 內第一次出現之以零為起始的索引。Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List<T>.

FindIndex(Int32, Predicate<T>) FindIndex(Int32, Predicate<T>) FindIndex(Int32, Predicate<T>) FindIndex(Int32, Predicate<T>)

搜尋符合指定之述詞所定義的條件之項目,並傳回 List<T> 內 (從指定之索引延伸到最後一個項目),於某項目範圍中第一次出現之以零為起始的索引。Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<T> that extends from the specified index to the last element.

FindIndex(Int32, Int32, Predicate<T>) FindIndex(Int32, Int32, Predicate<T>) FindIndex(Int32, Int32, Predicate<T>) FindIndex(Int32, Int32, Predicate<T>)

搜尋符合指定之述詞所定義的條件之項目,並傳回 List<T> 中從指定之索引開始,且包含指定之項目數目的項目範圍內第一個符合項目之以零為起始的索引。Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<T> that starts at the specified index and contains the specified number of elements.

FindIndex(Predicate<T>) FindIndex(Predicate<T>) FindIndex(Predicate<T>) FindIndex(Predicate<T>)

搜尋符合指定之述詞所定義的條件之項目,並傳回整個 List<T> 內第一次出現之以零為起始的索引。Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire 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> Predicate<T> Predicate<T>

定義要搜尋元素之條件的 Predicate<T> 委派。The Predicate<T> delegate that defines the conditions of the element to search for.

傳回

第一次出現符合 match 所定義之條件的項目以零為起始的索引 (如有找到);否則為 -1。The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

例外狀況

範例

下列範例會定義Employee類別的兩個欄位NameIdThe following example defines an Employee class with two fields, Name and Id. 它也會定義EmployeeSearch類別具有單一方法StartsWith,表示是否Employee.Name欄位的開頭指定的子字串提供給EmployeeSearch類別建構函式。It also defines an EmployeeSearch class with a single method, StartsWith, that indicates whether the Employee.Name field starts with a specified substring that is supplied to the EmployeeSearch class constructor. 請注意此方法的簽章Note the signature of this method

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

對應至可傳遞至委派的簽章FindIndex方法。corresponds to the signature of the delegate that can be passed to the FindIndex method. 此範例會具現化List<Employee>物件,加入一些Employee物件,然後再呼叫FindIndex(Int32, Int32, Predicate<T>)方法兩次來搜尋整個集合,也就是第一個第一次Employee物件,其Name欄位開始"J",與第二次的前Employee物件,其Name欄位開頭為"Ju"。The example instantiates a List<Employee> object, adds a number of Employee objects to it, and then calls the FindIndex(Int32, Int32, Predicate<T>) method twice to search the entire collection, the first time for the first Employee object whose Name field begins with "J", and the second time for the first Employee object whose Name field begins with "Ju".

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>向前第一個項目開始和結束時間的最後一個項目搜尋。The List<T> is searched forward starting at the first element and ending at the last element.

Predicate<T>會傳回方法的委派true物件傳遞給是否符合委派中定義的條件。The Predicate<T> is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. 目前的項目List<T>個別傳遞給Predicate<T>委派。The elements of the current List<T> are individually passed to the Predicate<T> delegate. 委派的簽章:The delegate has the signature:

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

這個方法會執行線性搜尋;因此,這個方法是 O (n) 運算,其中nCountThis method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

另請參閱

FindIndex(Int32, Predicate<T>) FindIndex(Int32, Predicate<T>) FindIndex(Int32, Predicate<T>) FindIndex(Int32, Predicate<T>)

搜尋符合指定之述詞所定義的條件之項目,並傳回 List<T> 內 (從指定之索引延伸到最後一個項目),於某項目範圍中第一次出現之以零為起始的索引。Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<T> that extends from the specified index to the last element.

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 Int32 Int32 Int32

搜尋之以零為起始的起始索引。The zero-based starting index of the search.

match
Predicate<T> Predicate<T> Predicate<T> Predicate<T>

定義要搜尋元素之條件的 Predicate<T> 委派。The Predicate<T> delegate that defines the conditions of the element to search for.

傳回

第一次出現符合 match 所定義之條件的項目以零為起始的索引 (如有找到);否則為 -1。The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

例外狀況

startIndex 超出 List<T> 的有效索引範圍。startIndex is outside the range of valid indexes for the List<T>.

範例

下列範例會定義Employee類別的兩個欄位NameIdThe following example defines an Employee class with two fields, Name and Id. 它也會定義EmployeeSearch類別具有單一方法StartsWith,表示是否Employee.Name欄位的開頭指定的子字串提供給EmployeeSearch類別建構函式。It also defines an EmployeeSearch class with a single method, StartsWith, that indicates whether the Employee.Name field starts with a specified substring that is supplied to the EmployeeSearch class constructor. 請注意此方法的簽章Note the signature of this method

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

對應至可傳遞至委派的簽章FindIndex方法。corresponds to the signature of the delegate that can be passed to the FindIndex method. 此範例會具現化List<Employee>物件,加入一些Employee物件,然後再呼叫FindIndex(Int32, Int32, Predicate<T>)方法兩次來搜尋開頭為其第五個成員 (也就是索引 4 成員) 的集合。The example instantiates a List<Employee> object, adds a number of Employee objects to it, and then calls the FindIndex(Int32, Int32, Predicate<T>) method twice to search the collection starting with its fifth member (that is, the member at index 4). 第一次,它會搜尋第一個Employee物件,其Name欄位開頭為"J"; 第二次,它會搜尋第一個Employee物件,其Name欄位開頭為"Ju"。The first time, it searches for the first Employee object whose Name field begins with "J"; the second time, it searches for the first Employee object whose Name field begins with "Ju".

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並結束於最後一個項目。The List<T> is searched forward starting at startIndex and ending at the last element.

Predicate<T>會傳回方法的委派true物件傳遞給是否符合委派中定義的條件。The Predicate<T> is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. 目前的項目List<T>個別傳遞給Predicate<T>委派。The elements of the current List<T> are individually passed to the Predicate<T> delegate. 委派的簽章:The delegate has the signature:

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

這個方法會執行線性搜尋;因此,這個方法是 O (n) 運算,其中n中的項目數目startIndex結尾List<T>This method performs a linear search; therefore, this method is an O(n) operation, where n is the number of elements from startIndex to the end of the List<T>.

另請參閱

FindIndex(Int32, Int32, Predicate<T>) FindIndex(Int32, Int32, Predicate<T>) FindIndex(Int32, Int32, Predicate<T>) FindIndex(Int32, Int32, Predicate<T>)

搜尋符合指定之述詞所定義的條件之項目,並傳回 List<T> 中從指定之索引開始,且包含指定之項目數目的項目範圍內第一個符合項目之以零為起始的索引。Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<T> that starts at the specified index and contains the specified number of elements.

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 Int32 Int32 Int32

搜尋之以零為起始的起始索引。The zero-based starting index of the search.

count
Int32 Int32 Int32 Int32

區段中要搜尋的項目數目。The number of elements in the section to search.

match
Predicate<T> Predicate<T> Predicate<T> Predicate<T>

定義要搜尋元素之條件的 Predicate<T> 委派。The Predicate<T> delegate that defines the conditions of the element to search for.

傳回

第一次出現符合 match 所定義之條件的項目以零為起始的索引 (如有找到);否則為 -1。The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

例外狀況

startIndex 超出 List<T> 的有效索引範圍。startIndex is outside the range of valid indexes for the List<T>.

-或--or- count 小於 0。count is less than 0.

-或--or- startIndexcount 不指定 List<T> 的有效區段。startIndex and count do not specify a valid section in the List<T>.

範例

下列範例會定義Employee類別的兩個欄位NameIdThe following example defines an Employee class with two fields, Name and Id. 它也會定義EmployeeSearch類別具有單一方法StartsWith,表示是否Employee.Name欄位的開頭指定的子字串提供給EmployeeSearch類別建構函式。It also defines an EmployeeSearch class with a single method, StartsWith, that indicates whether the Employee.Name field starts with a specified substring that is supplied to the EmployeeSearch class constructor. 請注意此方法的簽章Note the signature of this method

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

對應至可傳遞至委派的簽章FindIndex方法。corresponds to the signature of the delegate that can be passed to the FindIndex method. 此範例會具現化List<Employee>物件,加入一些Employee物件,然後再呼叫FindIndex(Int32, Int32, Predicate<T>)方法兩次來搜尋整個集合 (也就是成員從索引 0 到索引Count-1)。The example instantiates a List<Employee> object, adds a number of Employee objects to it, and then calls the FindIndex(Int32, Int32, Predicate<T>) method twice to search the entire collection (that is, the members from index 0 to index Count - 1). 第一次,它會搜尋第一個Employee物件,其Name欄位開頭為"J"; 第二次,它會搜尋第一個Employee物件,其Name欄位開頭為"Ju"。The first time, it searches for the first Employee object whose Name field begins with "J"; the second time, it searches for the first Employee object whose Name field begins with "Ju".

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>往前開始搜尋startIndex結尾startIndex加上count減 1,如果count大於 0。The List<T> is searched forward starting at startIndex and ending at startIndex plus count minus 1, if count is greater than 0.

Predicate<T>會傳回方法的委派true物件傳遞給是否符合委派中定義的條件。The Predicate<T> is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. 目前的項目List<T>個別傳遞給Predicate<T>委派。The elements of the current List<T> are individually passed to the Predicate<T> delegate. 委派的簽章:The delegate has the signature:

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

這個方法會執行線性搜尋;因此,這個方法是 O (n) 運算,其中ncountThis method performs a linear search; therefore, this method is an O(n) operation, where n is count.

另請參閱

適用於