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
- 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 到索引 Count - 1 的成员)。 第一次,它会搜索字段Employee以“J”开头的第一个Name对象;第二次搜索其Employee字段以“Ju”开头的第一个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
注解
List<T>如果大于 0,startIndex则从加startIndex减 1 开始count和结束count搜索前方。
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
- 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>)其Employee字段以“J”开头的第Name一个对象,第二次调用其Employee字段以“Ju”开头的第一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
- 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 上的成员)。 第一次,它会搜索字段Employee以“J”开头的第一个Name对象;第二次搜索其Employee字段以“Ju”开头的第一个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>