List<T>.FindIndex メソッド

定義

指定した述語で定義された条件に一致する要素を検索し、 List<T> またはその一部内で最初に見つかった位置の 0 から始まるインデックスを返します。 このメソッドは、条件に一致する項目が見つからない場合に -1 を返します。

オーバーロード

名前 説明
FindIndex(Predicate<T>)

指定した述語で定義されている条件に一致する要素を検索し、 List<T>全体で最初に見つかった位置の 0 から始まるインデックスを返します。

FindIndex(Int32, Predicate<T>)

指定した述語で定義された条件に一致する要素を検索し、指定したインデックスから最後の要素まで拡張される List<T> 内の要素の範囲内で最初に出現する 0 から始まるインデックスを返します。

FindIndex(Int32, Int32, Predicate<T>)

指定した述語で定義された条件に一致する要素を検索し、指定したインデックスから始まり、指定した数の要素を含む List<T> 内の要素の範囲内で最初に出現する 0 から始まるインデックスを返します。

FindIndex(Predicate<T>)

指定した述語で定義されている条件に一致する要素を検索し、 List<T>全体で最初に見つかった位置の 0 から始まるインデックスを返します。

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によって定義された条件に一致する要素が最初に見つかった場合は 0 から始まるインデックス。それ以外の場合は -1。

例外

matchnullです。

次の例では、NameId の 2 つのフィールドを持つEmployee クラスを定義します。 また、Employee.Name フィールドが、EmployeeSearch クラス コンストラクターに指定された指定された部分文字列で始まるかどうかを示す 1 つのメソッド (StartsWith) を持つEmployeeSearch クラスも定義します。 このメソッドのシグネチャに注意してください

public bool StartsWith(Employee e)
Public Function StartsWith(e As Employee) As Boolean

は、 FindIndex メソッドに渡すことができるデリゲートのシグネチャに対応します。 この例では、List<Employee> オブジェクトをインスタンス化し、Employee オブジェクトを多数追加した後、FindIndex(Int32, Int32, Predicate<T>) メソッドを 2 回呼び出してコレクション全体を検索し、Name フィールドが "J" で始まる最初のEmployee オブジェクトを初めて、Name フィールドが "Ju" で始まる最初のEmployee オブジェクトを 2 回呼び出します。

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<T> 内の要素の範囲内で最初に出現する 0 から始まるインデックスを返します。

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

検索の 0 から始まる開始インデックス。

match
Predicate<T>

検索する要素の条件を定義する Predicate<T> デリゲート。

返品

matchによって定義された条件に一致する要素が最初に見つかった場合は 0 から始まるインデックス。それ以外の場合は -1。

例外

matchnullです。

startIndex が、 List<T>の有効なインデックスの範囲外です。

次の例では、NameId の 2 つのフィールドを持つEmployee クラスを定義します。 また、Employee.Name フィールドが、EmployeeSearch クラス コンストラクターに指定された指定された部分文字列で始まるかどうかを示す 1 つのメソッド (StartsWith) を持つEmployeeSearch クラスも定義します。 このメソッドのシグネチャに注意してください

public bool StartsWith(Employee e)
Public Function StartsWith(e As Employee) As Boolean

は、 FindIndex メソッドに渡すことができるデリゲートのシグネチャに対応します。 この例では、 List<Employee> オブジェクトをインスタンス化し、 Employee オブジェクトの数を追加した後、 FindIndex(Int32, Int32, Predicate<T>) メソッドを 2 回呼び出して、5 番目のメンバー (インデックス 4 のメンバー) で始まるコレクションを検索します。 最初に、Name フィールドが "J" で始まる最初のEmployee オブジェクトを検索します。2 回目は、Name フィールドが "Ju" で始まる最初のEmployee オブジェクトを検索します。

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) 演算であり、 nstartIndex から List<T>の末尾までの要素の数です。

こちらもご覧ください

適用対象

FindIndex(Int32, Int32, Predicate<T>)

指定した述語で定義された条件に一致する要素を検索し、指定したインデックスから始まり、指定した数の要素を含む List<T> 内の要素の範囲内で最初に出現する 0 から始まるインデックスを返します。

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

検索の 0 から始まる開始インデックス。

count
Int32

検索するセクション内の要素の数。

match
Predicate<T>

検索する要素の条件を定義する Predicate<T> デリゲート。

返品

matchによって定義された条件に一致する要素が最初に見つかった場合は 0 から始まるインデックス。それ以外の場合は -1。

例外

matchnullです。

startIndex が、 List<T>の有効なインデックスの範囲外です。

-または-

count が 0 未満です。

-または-

startIndex countは、List<T>で有効なセクションを指定しません。

次の例では、NameId の 2 つのフィールドを持つEmployee クラスを定義します。 また、Employee.Name フィールドが、EmployeeSearch クラス コンストラクターに指定された指定された部分文字列で始まるかどうかを示す 1 つのメソッド (StartsWith) を持つEmployeeSearch クラスも定義します。 このメソッドのシグネチャに注意してください

public bool StartsWith(Employee e)
Public Function StartsWith(e As Employee) As Boolean

は、 FindIndex メソッドに渡すことができるデリゲートのシグネチャに対応します。 この例では、 List<Employee> オブジェクトをインスタンス化し、 Employee オブジェクトの数を追加した後、 FindIndex(Int32, Int32, Predicate<T>) メソッドを 2 回呼び出してコレクション全体を検索します (つまり、インデックス 0 からインデックス Count - 1 までのメンバー)。 最初に、Name フィールドが "J" で始まる最初のEmployee オブジェクトを検索します。2 回目は、Name フィールドが "Ju" で始まる最初のEmployee オブジェクトを検索します。

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

注釈

countが 0 より大きい場合、List<T>startIndex から始まり、startIndex + count - 1 で終わる順に検索されます。

Predicate<T>は、渡されたオブジェクトがデリゲートで定義されている条件と一致する場合にtrueを返すメソッドへのデリゲートです。 現在の List<T> の要素は、 Predicate<T> デリゲートに個別に渡されます。 デリゲートには、次の署名があります。

public bool methodName(T obj)
Public Function methodName(obj As T) As Boolean

このメソッドは線形検索を実行します。したがって、このメソッドは O(n) 演算であり、 ncount

こちらもご覧ください

適用対象