List<T>.FindIndex 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
搜尋符合指定述詞所定義之條件的項目,並傳回 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。
例外狀況
match
為 null
。
範例
下列範例會定義具有兩個 Employee
欄位 Name
和 Id
的類別。 它也會使用單一方法定義 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 到 index Count - 1) 的成員。 第一次,它會搜尋欄位開頭為 「J」 的第一個 Employee
物件 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(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
備註
如果 大於 0,則會 List<T> 從 開始 startIndex
搜尋 ,結尾為 startIndex
加 count
減 count
1。
Predicate<T>是方法的委派,如果傳遞給它的物件符合委派中定義的條件,則會傳回 true
。 目前 List<T> 的專案會個別傳遞至 Predicate<T> 委派。 委派具有簽章:
public bool methodName(T obj)
Public Function methodName(obj As T) As Boolean
這個方法會執行線性搜尋;因此,這個方法是 o (n) 作業,其中 n 是 count
。
另請參閱
- Exists(Predicate<T>)
- Find(Predicate<T>)
- FindLast(Predicate<T>)
- FindAll(Predicate<T>)
- FindLastIndex
- BinarySearch
- IndexOf
- LastIndexOf
- Predicate<T>
適用於
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。
例外狀況
match
為 null
。
範例
下列範例會定義具有兩個 Employee
欄位 Name
和 Id
的類別。 它也會使用單一方法定義 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」 的第一個物件,第二次呼叫欄位開頭為 「Ju」 的第一 Employee
個 Employee
物件 Name
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) 作業,其中 n 是 Count 。
另請參閱
- Exists(Predicate<T>)
- Find(Predicate<T>)
- FindLast(Predicate<T>)
- FindAll(Predicate<T>)
- FindLastIndex
- BinarySearch
- IndexOf
- LastIndexOf
- Predicate<T>
適用於
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。
例外狀況
match
為 null
。
startIndex
超出 List<T> 的有效索引範圍。
範例
下列範例會定義具有兩個 Employee
欄位 Name
和 Id
的類別。 它也會使用單一方法定義 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) 的成員。 第一次,它會搜尋欄位開頭為 「J」 的第一個 Employee
物件 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");
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> 數目。
另請參閱
- Exists(Predicate<T>)
- Find(Predicate<T>)
- FindLast(Predicate<T>)
- FindAll(Predicate<T>)
- FindLastIndex
- BinarySearch
- IndexOf
- LastIndexOf
- Predicate<T>
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應