List<T>.FindIndex 메서드

정의

지정된 조건자에 정의된 조건과 일치하는 요소를 검색하고 List<T>이나 그 일부에서 처음으로 검색한 요소의 인덱스(0부터 시작)를 반환합니다. 이 메서드는 조건이 일치하는 항목이 없는 경우 -1을 반환합니다.

오버로드

FindIndex(Int32, Int32, Predicate<T>)

지정된 조건자에 정의된 조건과 일치하는 요소를 검색하고 지정된 인덱스부터 시작하여 지정된 수의 요소를 포함하는 List<T>의 요소 범위에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

FindIndex(Predicate<T>)

지정된 조건자에 정의된 조건과 일치하는 요소를 검색하여 전체 List<T>에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

FindIndex(Int32, Predicate<T>)

지정된 조건자에 정의된 조건과 일치하는 요소를 검색하여 지정된 인덱스에서 마지막 요소로 확장하는 List<T>의 요소 범위에서 일치하는 요소 중 첫 번째 요소의 인덱스(0부터 시작)를 반환합니다.

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입니다.

예외

match이(가) null인 경우

startIndexList<T>의 유효한 인덱스 범위를 벗어납니다.

또는

count 가 0보다 작습니다.

또는

startIndexcountList<T>의 올바른 섹션을 지정하지 않습니다.

예제

다음 예제에서는 및 필드가 두 개 있는 Employee 클래스를 NameId정의합니다. 또한 필드가 EmployeeSearch 클래스 생성자에 제공된 지정된 부분 문자열로 시작하는지 여부를 Employee.Name 나타내는 단일 메서드 StartsWith를 사용하여 클래스를 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)을 검색합니다. 처음으로 필드가 Name "J"로 시작하는 첫 번째 Employee 개체를 검색하고, 두 번째로 필드가 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

설명

List<T> 0보다 큰 경우 count 는 에서 startIndex 시작하여 1을 더한 countstartIndex 으로 끝나는 앞으로 검색됩니다.

Predicate<T> 전달된 개체가 대리자에서 정의된 조건과 일치하는 경우 를 반환 true 하는 메서드에 대한 대리자입니다. 현재 List<T> 의 요소는 개별적으로 대리자에게 Predicate<T> 전달됩니다. 대리자의 서명은 다음과 같습니다.

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

이 메서드는 선형 검색을 수행합니다. 따라서 이 메서드는 O(n) 작업이며 여기서 n 은 입니다 count.

추가 정보

적용 대상

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입니다.

예외

match이(가) null인 경우

예제

다음 예제에서는 및 필드가 두 개 있는 Employee 클래스를 NameId정의합니다. 또한 필드가 EmployeeSearch 클래스 생성자에 제공된 지정된 부분 문자열로 시작하는지 여부를 Employee.Name 나타내는 단일 메서드 StartsWith를 사용하여 클래스를 EmployeeSearch 정의합니다. 이 메서드의 서명을 확인합니다.

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

는 메서드에 전달할 수 있는 대리자의 서명에 해당합니다 FindIndex . 이 예제에서는 개체를 List<Employee> 인스턴스화하고 여러 개체를 추가 Employee 한 다음 메서드를 두 번 호출 FindIndex(Int32, Int32, Predicate<T>) 하여 전체 컬렉션을 검색하고, 필드가 Name "J"로 시작하는 첫 번째 Employee 개체와 필드가 "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(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.

추가 정보

적용 대상

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입니다.

예외

match이(가) null인 경우

startIndexList<T>의 유효한 인덱스 범위를 벗어납니다.

예제

다음 예제에서는 및 필드가 두 개 있는 Employee 클래스를 NameId정의합니다. 또한 필드가 EmployeeSearch 클래스 생성자에 제공된 지정된 부분 문자열로 시작하는지 여부를 Employee.Name 나타내는 단일 메서드 StartsWith를 사용하여 클래스를 EmployeeSearch 정의합니다. 이 메서드의 서명을 확인합니다.

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

는 메서드에 전달할 수 있는 대리자의 서명에 해당합니다 FindIndex . 이 예제에서는 개체를 List<Employee> 인스턴스화하고 여러 개체를 추가 Employee 한 다음 메서드를 두 번 호출 FindIndex(Int32, Int32, Predicate<T>) 하여 다섯 번째 멤버(즉, 인덱스 4의 멤버)로 시작하는 컬렉션을 검색합니다. 처음으로 필드가 Name "J"로 시작하는 첫 번째 Employee 개체를 검색하고, 두 번째로 필드가 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) 연산입니다. 여기서 n은 의 List<T>에서 startIndex 끝까지의 요소 수입니다.

추가 정보

적용 대상