List<T>.FindIndex 메서드

정의

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

오버로드

Name Description
FindIndex(Predicate<T>)

지정된 조건자에서 정의한 조건과 일치하는 요소를 검색하고 전체 List<T>내에서 처음 발생하는 0부터 시작하는 인덱스(0부터 시작)를 반환합니다.

FindIndex(Int32, Predicate<T>)

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

FindIndex(Int32, Int32, Predicate<T>)

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

FindIndex(Predicate<T>)

지정된 조건자에서 정의한 조건과 일치하는 요소를 검색하고 전체 List<T>내에서 처음 발생하는 0부터 시작하는 인덱스(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입니다.

예제

다음 예제에서는 두 필드가 있는 Employee 클래스를 정의하고 Id. Name 또한 필드가 클래스 생성자에 제공된 지정된 부분 문자열로 시작하는지 여부를 Employee.Name 나타내는 단일 메서드StartsWith를 사용하여 클래스를 EmployeeSearch 정의 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입니다.

예외

matchnull입니다.

startIndex 가 .에 대한 유효한 인덱스 범위를 벗어났습니다 List<T>.

예제

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

-또는-

startIndexcount 유효한 섹션 List<T>을 지정하지 마세요.

예제

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

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

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

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

추가 정보

적용 대상