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

定義

List<T> またはその一部分から、指定した述語によって定義される条件に一致する要素を検索し、最もインデックス番号の小さい要素の 0 から始まるインデックスを返します。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> 全体から、指定した述語によって定義される条件に一致する要素を検索し、最もインデックス番号の小さい要素の 0 から始まるインデックスを返します。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> の指定したインデックスから最後の要素までの範囲内で、指定した述語にで定義される条件に一致する要素を検索し、最初に見つかった 0 から始まるインデックスを返します。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> のうち、指定したインデックスから始まり、指定した要素数が含まれる範囲の中で、指定した述語によって定義される条件に一致する要素を検索し、そのうち最もインデックス番号の小さい要素の 0 から始まるインデックスを返します。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> 全体から、指定した述語によって定義される条件に一致する要素を検索し、最もインデックス番号の小さい要素の 0 から始まるインデックスを返します。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 で定義された条件と一致する要素が存在した場合、最もインデックス番号の小さい要素の 0 から始まるインデックス。それ以外の場合は -1。The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

例外

次の例では、定義、 Employee 、2 つのフィールドを持つクラスNameIdします。The following example defines an Employee class with two fields, Name and Id. 定義されています、 EmployeeSearch 1 つのメソッドを持つクラス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>)メソッドは、最初の最初に、コレクション全体を検索するには、2 回EmployeeオブジェクトNameフィールドの開始"J"と、最初の 2 回目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) 操作、 nCountします。This 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> の指定したインデックスから最後の要素までの範囲内で、指定した述語にで定義される条件に一致する要素を検索し、最初に見つかった 0 から始まるインデックスを返します。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

検索の開始位置を示す 0 から始まるインデックス。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 で定義された条件と一致する要素が存在した場合、最もインデックス番号の小さい要素の 0 から始まるインデックス。それ以外の場合は -1。The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

例外

startIndexList<T> の有効なインデックスの範囲外です。startIndex is outside the range of valid indexes for the List<T>.

次の例では、定義、 Employee 、2 つのフィールドを持つクラスNameIdします。The following example defines an Employee class with two fields, Name and Id. 定義されています、 EmployeeSearch 1 つのメソッドを持つクラス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>)以降その 5 番目のメンバー (つまり、4 のインデックス位置にあるメンバー) でコレクションを検索するには、2 回のメソッド。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> のうち、指定したインデックスから始まり、指定した要素数が含まれる範囲の中で、指定した述語によって定義される条件に一致する要素を検索し、そのうち最もインデックス番号の小さい要素の 0 から始まるインデックスを返します。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

検索の開始位置を示す 0 から始まるインデックス。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 で定義された条件と一致する要素が存在した場合、最もインデックス番号の小さい要素の 0 から始まるインデックス。それ以外の場合は -1。The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

例外

startIndexList<T> の有効なインデックスの範囲外です。startIndex is outside the range of valid indexes for the List<T>.

または-or- count が 0 未満です。count is less than 0.

または-or- startIndex および countList<T> 内の正しいセクションを指定していません。startIndex and count do not specify a valid section in the List<T>.

次の例では、定義、 Employee 、2 つのフィールドを持つクラスNameIdします。The following example defines an Employee class with two fields, Name and Id. 定義されています、 EmployeeSearch 1 つのメソッドを持つクラス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>)コレクション全体を検索するには、2 回のメソッド (インデックスをインデックス 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で終わるstartIndexpluscountから 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) 操作、 ncountします。This method performs a linear search; therefore, this method is an O(n) operation, where n is count.

こちらもご覧ください

適用対象