語言

List<T>.FindIndex 方法

定義

搜尋符合指定謂詞定義條件的元素,並回傳該稱詞或部分中 List<T> 首次出現的零為基礎索引。 如果找不到符合條件的項目,此方法會回傳 -1。

多載

名稱 Description
FindIndex(Int32, Int32, Predicate<T>)

搜尋符合指定謂詞定義條件的元素,並回傳從指定索引開始且包含指定數量元素的元素範圍內首次出現 List<T> 的零為基礎的索引。

FindIndex(Predicate<T>)

搜尋符合指定謂詞定義條件的元素,並回傳整個 List<T>中首次出現的零基索引。

FindIndex(Int32, Predicate<T>)

搜尋符合指定謂詞定義條件的元素,並回傳從指定索引延伸到最後一個元素範圍內,首次出現 List<T> 的零為基礎的索引。

FindIndex(Int32, Int32, Predicate<T>)

來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
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 的成員)。 第一次,它搜尋第一個 EmployeeName 以「J」開頭的物體;第二次,它搜尋第一個 EmployeeName 以「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 大於 0 時,則從 開始startIndex向前搜尋,直到正countcount負 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>)

來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
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>)呼叫該方法兩次搜尋整個集合,第一次是搜尋欄位Employee以「J」開頭的第一個Name物件,第二次則是搜尋欄位以「Ju」開頭的第一個Employee物件Name

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

來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
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 方法的代理簽名。 範例中實例化一個 List<Employee> 物件,向其加入若干 Employee 物件,然後呼叫 FindIndex(Int32, Int32, Predicate<T>) 該方法兩次,從第五個成員(即索引 4 的成員)開始搜尋集合。 第一次,它搜尋第一個 EmployeeName 以「J」開頭的物體;第二次,它搜尋第一個 EmployeeName 以「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向前搜尋,直到最後一個元素。

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>端的元素數。

另請參閱

適用於