List<T>.FindIndex Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Recherche un élément qui correspond aux conditions définies par un prédicat spécifié et retourne l’index de base zéro de la première occurrence trouvée dans le List<T> ou une partie de celui-ci. Cette méthode retourne -1 si un élément correspondant aux conditions est introuvable.
Surcharges
FindIndex(Int32, Int32, Predicate<T>) |
Recherche un élément qui correspond aux conditions définies par le prédicat spécifié et retourne l'index de base zéro de la première occurrence trouvée dans la plage d'éléments de List<T> qui commence à l'index spécifié et contient le nombre d'éléments spécifié. |
FindIndex(Predicate<T>) |
Recherche un élément qui correspond aux conditions définies par le prédicat spécifié et retourne l'index de base zéro de la première occurrence trouvée dans le List<T> entier. |
FindIndex(Int32, Predicate<T>) |
Recherche un élément qui correspond aux conditions définies par le prédicat spécifié et retourne l'index de base zéro de la première occurrence trouvée dans la plage d'éléments de List<T> qui s'étend de l'index spécifié au dernier élément. |
FindIndex(Int32, Int32, Predicate<T>)
Recherche un élément qui correspond aux conditions définies par le prédicat spécifié et retourne l'index de base zéro de la première occurrence trouvée dans la plage d'éléments de List<T> qui commence à l'index spécifié et contient le nombre d'éléments spécifié.
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
Paramètres
- startIndex
- Int32
Index de début de base zéro de la recherche.
- count
- Int32
Nombre d’éléments contenus dans la section où la recherche doit être effectuée.
- match
- Predicate<T>
Délégué Predicate<T> qui définit les conditions de l'élément à rechercher.
Retours
Index de base zéro de la première occurrence d’un élément qui répond aux conditions définies par match
, si cette occurrence est trouvée ; sinon, -1.
Exceptions
match
a la valeur null
.
startIndex
est en dehors de la plage d’index valides pour List<T>.
ou -
count
est inférieur à 0.ou -
startIndex
etcount
ne spécifient pas une section valide dans List<T>.
Exemples
L’exemple suivant définit une Employee
classe avec deux champs et Id``Name
. Il définit également une EmployeeSearch
classe avec une méthode unique, StartsWith
qui indique si le Employee.Name
champ commence par une sous-chaîne spécifiée fournie au EmployeeSearch
constructeur de classe. Notez la signature de cette méthode
public bool StartsWith(Employee e)
Public Function StartsWith(e As Employee) As Boolean
correspond à la signature du délégué qui peut être transmis à la FindIndex méthode. L’exemple instancie un List<Employee>
objet, ajoute un certain nombre d’objets Employee
à celui-ci, puis appelle FindIndex(Int32, Int32, Predicate<T>) la méthode deux fois pour rechercher la collection entière (autrement dit, les membres de l’index 0 à l’index Count - 1). La première fois, il recherche le premier Employee
objet dont Name
le champ commence par « J » ; la deuxième fois, il recherche le premier Employee
objet dont Name
le champ commence par « 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
Remarques
La List<T> recherche est effectuée avant à partir et startIndex
se termine à startIndex
plus count
moins 1, si count
elle est supérieure à 0.
Il Predicate<T> s’agit d’un délégué à une méthode qui retourne true
si l’objet passé à celui-ci correspond aux conditions définies dans le délégué. Les éléments du courant List<T> sont transmis individuellement au Predicate<T> délégué. Le délégué a la signature :
public bool methodName(T obj)
Public Function methodName(obj As T) As Boolean
Cette méthode effectue une recherche linéaire ; par conséquent, cette méthode est une opération O(n), où n est count
.
Voir aussi
- Exists(Predicate<T>)
- Find(Predicate<T>)
- FindLast(Predicate<T>)
- FindAll(Predicate<T>)
- FindLastIndex
- BinarySearch
- IndexOf
- LastIndexOf
- Predicate<T>
S’applique à
FindIndex(Predicate<T>)
Recherche un élément qui correspond aux conditions définies par le prédicat spécifié et retourne l'index de base zéro de la première occurrence trouvée dans le List<T> entier.
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
Paramètres
- match
- Predicate<T>
Délégué Predicate<T> qui définit les conditions de l'élément à rechercher.
Retours
Index de base zéro de la première occurrence d’un élément qui répond aux conditions définies par match
, si cette occurrence est trouvée ; sinon, -1.
Exceptions
match
a la valeur null
.
Exemples
L’exemple suivant définit une Employee
classe avec deux champs et Id``Name
. Il définit également une EmployeeSearch
classe avec une méthode unique, StartsWith
qui indique si le Employee.Name
champ commence par une sous-chaîne spécifiée fournie au EmployeeSearch
constructeur de classe. Notez la signature de cette méthode
public bool StartsWith(Employee e)
Public Function StartsWith(e As Employee) As Boolean
correspond à la signature du délégué qui peut être transmis à la FindIndex méthode. L’exemple instancie un List<Employee>
objet, ajoute un certain nombre d’objets Employee
à celui-ci, puis appelle la méthode deux fois pour rechercher l’intégralité de la collection, la première fois pour le premier Employee
objet dont Name
le champ commence par « J », et la deuxième fois pour le premier Employee
objet dont Name
le FindIndex(Int32, Int32, Predicate<T>) champ commence par « 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
Remarques
La List<T> recherche est effectuée avant à partir du premier élément et se termine au dernier élément.
Il Predicate<T> s’agit d’un délégué à une méthode qui retourne true
si l’objet passé à celui-ci correspond aux conditions définies dans le délégué. Les éléments du courant List<T> sont transmis individuellement au Predicate<T> délégué. Le délégué a la signature :
public bool methodName(T obj)
Public Function methodName(obj As T) As Boolean
Cette méthode effectue une recherche linéaire ; par conséquent, cette méthode est une opération O(n), où n est Count.
Voir aussi
- Exists(Predicate<T>)
- Find(Predicate<T>)
- FindLast(Predicate<T>)
- FindAll(Predicate<T>)
- FindLastIndex
- BinarySearch
- IndexOf
- LastIndexOf
- Predicate<T>
S’applique à
FindIndex(Int32, Predicate<T>)
Recherche un élément qui correspond aux conditions définies par le prédicat spécifié et retourne l'index de base zéro de la première occurrence trouvée dans la plage d'éléments de List<T> qui s'étend de l'index spécifié au dernier élément.
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
Paramètres
- startIndex
- Int32
Index de début de base zéro de la recherche.
- match
- Predicate<T>
Délégué Predicate<T> qui définit les conditions de l'élément à rechercher.
Retours
Index de base zéro de la première occurrence d’un élément qui répond aux conditions définies par match
, si cette occurrence est trouvée ; sinon, -1.
Exceptions
match
a la valeur null
.
startIndex
est en dehors de la plage d’index valides pour List<T>.
Exemples
L’exemple suivant définit une Employee
classe avec deux champs et Id``Name
. Il définit également une EmployeeSearch
classe avec une méthode unique, StartsWith
qui indique si le Employee.Name
champ commence par une sous-chaîne spécifiée fournie au EmployeeSearch
constructeur de classe. Notez la signature de cette méthode
public bool StartsWith(Employee e)
Public Function StartsWith(e As Employee) As Boolean
correspond à la signature du délégué qui peut être transmis à la FindIndex méthode. L’exemple instancie un List<Employee>
objet, ajoute un certain nombre d’objets Employee
à celui-ci, puis appelle la FindIndex(Int32, Int32, Predicate<T>) méthode deux fois pour rechercher la collection à partir de son cinquième membre (autrement dit, le membre à l’index 4). La première fois, il recherche le premier Employee
objet dont Name
le champ commence par « J » ; la deuxième fois, il recherche le premier Employee
objet dont Name
le champ commence par « 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
Remarques
La List<T> recherche est effectuée avant à startIndex
partir et se termine au dernier élément.
Il Predicate<T> s’agit d’un délégué à une méthode qui retourne true
si l’objet passé à celui-ci correspond aux conditions définies dans le délégué. Les éléments du courant List<T> sont transmis individuellement au Predicate<T> délégué. Le délégué a la signature :
public bool methodName(T obj)
Public Function methodName(obj As T) As Boolean
Cette méthode effectue une recherche linéaire ; par conséquent, cette méthode est une opération O(n), où n est le nombre d’éléments de startIndex
la fin du List<T>.
Voir aussi
- Exists(Predicate<T>)
- Find(Predicate<T>)
- FindLast(Predicate<T>)
- FindAll(Predicate<T>)
- FindLastIndex
- BinarySearch
- IndexOf
- LastIndexOf
- Predicate<T>