List<T>.FindIndex Metode

Definisi

Mencari elemen yang cocok dengan kondisi yang ditentukan oleh predikat tertentu, dan mengembalikan indeks berbasis nol dari kemunculan pertama dalam List<T> atau sebagiannya. Metode ini mengembalikan -1 jika item yang cocok dengan kondisi tidak ditemukan.

Overload

FindIndex(Int32, Int32, Predicate<T>)

Mencari elemen yang cocok dengan kondisi yang ditentukan oleh predikat yang ditentukan, dan mengembalikan indeks berbasis nol dari kemunculan pertama dalam rentang elemen dalam List<T> yang dimulai pada indeks yang ditentukan dan berisi jumlah elemen yang ditentukan.

FindIndex(Predicate<T>)

Mencari elemen yang cocok dengan kondisi yang ditentukan oleh predikat yang ditentukan, dan mengembalikan indeks berbasis nol dari kemunculan pertama di seluruh List<T>.

FindIndex(Int32, Predicate<T>)

Mencari elemen yang cocok dengan kondisi yang ditentukan oleh predikat yang ditentukan, dan mengembalikan indeks berbasis nol dari kemunculan pertama dalam rentang elemen dalam List<T> yang meluas dari indeks yang ditentukan ke elemen terakhir.

FindIndex(Int32, Int32, Predicate<T>)

Sumber:
List.cs
Sumber:
List.cs
Sumber:
List.cs

Mencari elemen yang cocok dengan kondisi yang ditentukan oleh predikat yang ditentukan, dan mengembalikan indeks berbasis nol dari kemunculan pertama dalam rentang elemen dalam List<T> yang dimulai pada indeks yang ditentukan dan berisi jumlah elemen yang ditentukan.

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

Parameter

startIndex
Int32

Indeks awal pencarian berbasis nol.

count
Int32

Jumlah elemen di bagian untuk dicari.

match
Predicate<T>

Predicate<T> Delegasi yang menentukan kondisi elemen yang akan dicari.

Mengembalikan

Indeks berbasis nol dari kemunculan pertama elemen yang cocok dengan kondisi yang ditentukan oleh match, jika ditemukan; jika tidak, -1.

Pengecualian

matchadalah null.

startIndex berada di luar rentang indeks yang valid untuk List<T>.

-atau-

count kurang dari 0.

-atau-

startIndex dan count jangan tentukan bagian yang valid di List<T>.

Contoh

Contoh berikut mendefinisikan Employee kelas dengan dua bidang, Name dan Id. Ini juga mendefinisikan EmployeeSearch kelas dengan satu metode, StartsWith, yang menunjukkan apakah Employee.Name bidang dimulai dengan substring tertentu yang disediakan ke EmployeeSearch konstruktor kelas. Perhatikan tanda tangan metode ini

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

sesuai dengan tanda tangan delegasi yang dapat diteruskan ke FindIndex metode . Contoh membuat instans List<Employee> objek, menambahkan sejumlah Employee objek ke dalamnya, lalu memanggil FindIndex(Int32, Int32, Predicate<T>) metode dua kali untuk mencari seluruh koleksi (yaitu, anggota dari indeks 0 ke indeks Count - 1). Pertama kali, ia mencari objek pertama Employee yang bidangnya Name dimulai dengan "J"; kedua kalinya, ia mencari objek pertama Employee yang bidangnya Name dimulai dengan "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

Keterangan

List<T> dicari ke depan mulai dari startIndex dan berakhir pada startIndex plus count minus 1, jika count lebih besar dari 0.

Predicate<T> adalah delegasi ke metode yang mengembalikan true jika objek yang diteruskan ke metode tersebut cocok dengan kondisi yang ditentukan dalam delegasi. Elemen saat ini List<T> diteruskan secara individual ke Predicate<T> delegasi. Delegasi memiliki tanda tangan:

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

Metode ini melakukan pencarian linier; oleh karena itu, metode ini adalah operasi O(n), di mana n adalah count.

Lihat juga

Berlaku untuk

FindIndex(Predicate<T>)

Sumber:
List.cs
Sumber:
List.cs
Sumber:
List.cs

Mencari elemen yang cocok dengan kondisi yang ditentukan oleh predikat yang ditentukan, dan mengembalikan indeks berbasis nol dari kemunculan pertama di seluruh 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

Parameter

match
Predicate<T>

Predicate<T> Delegasi yang menentukan kondisi elemen yang akan dicari.

Mengembalikan

Indeks berbasis nol dari kemunculan pertama elemen yang cocok dengan kondisi yang ditentukan oleh match, jika ditemukan; jika tidak, -1.

Pengecualian

matchadalah null.

Contoh

Contoh berikut mendefinisikan Employee kelas dengan dua bidang, Name dan Id. Ini juga mendefinisikan EmployeeSearch kelas dengan satu metode, StartsWith, yang menunjukkan apakah Employee.Name bidang dimulai dengan substring tertentu yang disediakan ke EmployeeSearch konstruktor kelas. Perhatikan tanda tangan metode ini

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

sesuai dengan tanda tangan delegasi yang dapat diteruskan ke FindIndex metode . Contoh membuat instans List<Employee> objek, menambahkan sejumlah Employee objek ke dalamnya, dan kemudian memanggil FindIndex(Int32, Int32, Predicate<T>) metode dua kali untuk mencari seluruh koleksi, pertama kali untuk objek pertama Employee yang bidangnya Name dimulai dengan "J", dan kedua kalinya untuk objek pertama Employee yang bidangnya Name dimulai dengan "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

Keterangan

List<T> dicari maju mulai dari elemen pertama dan berakhir di elemen terakhir.

Predicate<T> adalah delegasi ke metode yang mengembalikan true jika objek yang diteruskan ke metode tersebut cocok dengan kondisi yang ditentukan dalam delegasi. Elemen saat ini List<T> diteruskan secara individual ke Predicate<T> delegasi. Delegasi memiliki tanda tangan:

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

Metode ini melakukan pencarian linier; oleh karena itu, metode ini adalah operasi O(n), di mana n adalah Count.

Lihat juga

Berlaku untuk

FindIndex(Int32, Predicate<T>)

Sumber:
List.cs
Sumber:
List.cs
Sumber:
List.cs

Mencari elemen yang cocok dengan kondisi yang ditentukan oleh predikat yang ditentukan, dan mengembalikan indeks berbasis nol dari kemunculan pertama dalam rentang elemen dalam List<T> yang meluas dari indeks yang ditentukan ke elemen terakhir.

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

Parameter

startIndex
Int32

Indeks awal pencarian berbasis nol.

match
Predicate<T>

Predicate<T> Delegasi yang menentukan kondisi elemen yang akan dicari.

Mengembalikan

Indeks berbasis nol dari kemunculan pertama elemen yang cocok dengan kondisi yang ditentukan oleh match, jika ditemukan; jika tidak, -1.

Pengecualian

matchadalah null.

startIndex berada di luar rentang indeks yang valid untuk List<T>.

Contoh

Contoh berikut mendefinisikan Employee kelas dengan dua bidang, Name dan Id. Ini juga mendefinisikan EmployeeSearch kelas dengan satu metode, StartsWith, yang menunjukkan apakah Employee.Name bidang dimulai dengan substring tertentu yang disediakan ke EmployeeSearch konstruktor kelas. Perhatikan tanda tangan metode ini

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

sesuai dengan tanda tangan delegasi yang dapat diteruskan ke FindIndex metode . Contoh membuat instans List<Employee> objek, menambahkan sejumlah Employee objek ke dalamnya, lalu memanggil FindIndex(Int32, Int32, Predicate<T>) metode dua kali untuk mencari koleksi yang dimulai dengan anggota kelimanya (yaitu, anggota di indeks 4). Pertama kali, ia mencari objek pertama Employee yang bidangnya Name dimulai dengan "J"; kedua kalinya, ia mencari objek pertama Employee yang bidangnya Name dimulai dengan "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

Keterangan

List<T> dicari maju mulai dari dan startIndex berakhir pada elemen terakhir.

Predicate<T> adalah delegasi ke metode yang mengembalikan true jika objek yang diteruskan ke metode tersebut cocok dengan kondisi yang ditentukan dalam delegasi. Elemen saat ini List<T> diteruskan secara individual ke Predicate<T> delegasi. Delegasi memiliki tanda tangan:

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

Metode ini melakukan pencarian linier; oleh karena itu, metode ini adalah operasi O(n), di mana n adalah jumlah elemen dari startIndex hingga akhir List<T>.

Lihat juga

Berlaku untuk