List<T>.FindIndex Yöntem

Tanım

Belirtilen koşul tarafından tanımlanan koşullarla eşleşen bir öğeyi arar ve veya bir bölümü içinde List<T> ilk oluşumun sıfır tabanlı dizinini döndürür. Koşullarla eşleşen bir öğe bulunamazsa bu yöntem -1 döndürür.

Aşırı Yüklemeler

FindIndex(Int32, Int32, Predicate<T>)

Belirtilen koşul tarafından tanımlanan koşullarla eşleşen bir öğeyi arar ve içinde belirtilen dizinde başlayan ve belirtilen sayıda öğeyi içeren öğe List<T> aralığındaki ilk oluşumun sıfır tabanlı dizinini döndürür.

FindIndex(Predicate<T>)

Belirtilen koşul tarafından tanımlanan koşullarla eşleşen bir öğeyi arar ve tüm List<T>içindeki ilk oluşumun sıfır tabanlı dizinini döndürür.

FindIndex(Int32, Predicate<T>)

Belirtilen koşul tarafından tanımlanan koşullarla eşleşen bir öğeyi arar ve öğesindeki belirtilen dizinden son öğeye kadar uzanan öğe List<T> aralığındaki ilk oluşumun sıfır tabanlı dizinini döndürür.

FindIndex(Int32, Int32, Predicate<T>)

Source:
List.cs
Source:
List.cs
Source:
List.cs

Belirtilen koşul tarafından tanımlanan koşullarla eşleşen bir öğeyi arar ve içinde belirtilen dizinde başlayan ve belirtilen sayıda öğeyi içeren öğe List<T> aralığındaki ilk oluşumun sıfır tabanlı dizinini döndürür.

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

Parametreler

startIndex
Int32

Arama işleminin sıfır tabanlı başlangıç dizini.

count
Int32

Sıralanacak bölümdeki öğelerin sayısı.

match
Predicate<T>

Aranacak Predicate<T> öğenin koşullarını tanımlayan temsilci.

Döndürülenler

tarafından tanımlanan koşullarla matcheşleşen bir öğenin ilk oluşumunun sıfır tabanlı dizini; bulunursa, aksi takdirde -1.

Özel durumlar

match, null değeridir.

startIndex , için geçerli dizin aralığının dışındadır List<T>.

-veya-

count 0'dan küçüktür.

-veya-

startIndex ve count içinde List<T>geçerli bir bölüm belirtmeyin.

Örnekler

Aşağıdaki örnek, Name ve Idiki alanı olan bir Employee sınıfı tanımlar. Ayrıca, StartsWithalanın sınıf oluşturucusunun Employee.Name sağladığı EmployeeSearch belirtilen alt dizeyle başlayıp başlamadığını gösteren tek bir yöntemi olan bir sınıfı tanımlarEmployeeSearch. Bu yöntemin imzasını not edin

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

, yönteme geçirilebilen temsilcinin imzasına FindIndex karşılık gelir. Örnek bir List<Employee> nesnenin örneğini oluşturur, nesneye bir dizi Employee nesne ekler ve sonra koleksiyonun FindIndex(Int32, Int32, Predicate<T>) tamamında arama yapmak için yöntemini iki kez çağırır (diğer bir ifadeyle, dizin 0'dan dizine Count - 1'e üyeler). İlk kez, alanı "J" ile başlayan ilk Employee nesneyi Name arar; ikinci kez, alanı "Ju" ile başlayan ilk Employee nesneyi Name arar.

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

Açıklamalar

List<T>, 0'dan startIndex büyüksecount, artı eksi 1 ile countstartIndex başlayan ve biten ileriye doğru aranılır.

, Predicate<T> nesnesine geçirilen nesnenin temsilcide tanımlanan koşullarla eşleşmesi durumunda döndüren true bir yöntemin temsilcisidir. Geçerli List<T> öğenin öğeleri tek tek temsilciye Predicate<T> geçirilir. Temsilcinin imzası vardır:

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

Bu yöntem doğrusal bir arama gerçekleştirir; bu nedenle, bu yöntem bir O(n) işlemidir; burada n olur count.

Ayrıca bkz.

Şunlara uygulanır

FindIndex(Predicate<T>)

Source:
List.cs
Source:
List.cs
Source:
List.cs

Belirtilen koşul tarafından tanımlanan koşullarla eşleşen bir öğeyi arar ve tüm List<T>içindeki ilk oluşumun sıfır tabanlı dizinini döndürür.

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

Parametreler

match
Predicate<T>

Aranacak Predicate<T> öğenin koşullarını tanımlayan temsilci.

Döndürülenler

tarafından tanımlanan koşullarla matcheşleşen bir öğenin ilk oluşumunun sıfır tabanlı dizini; bulunursa, aksi takdirde -1.

Özel durumlar

match, null değeridir.

Örnekler

Aşağıdaki örnek, Name ve Idiki alanı olan bir Employee sınıfı tanımlar. Ayrıca, StartsWithalanın sınıf oluşturucusunun Employee.Name sağladığı EmployeeSearch belirtilen alt dizeyle başlayıp başlamadığını gösteren tek bir yöntemi olan bir sınıfı tanımlarEmployeeSearch. Bu yöntemin imzasını not edin

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

, yönteme geçirilebilen temsilcinin imzasına FindIndex karşılık gelir. Örnek bir List<Employee> nesnenin örneğini oluşturur, bir dizi Employee nesne ekler ve ardından tüm koleksiyonu aramak için yöntemini iki kez çağırırFindIndex(Int32, Int32, Predicate<T>); bu, alanı "J" ile başlayan ilk nesne Name için ilk Employee kez ve alanı "Ju" ile başlayan ilk Employee nesne Name için ikinci kez.

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

Açıklamalar

List<T>, ilk öğeden başlayıp son öğeyle biten ileriye doğru arandı.

, Predicate<T> nesnesine geçirilen nesnenin temsilcide tanımlanan koşullarla eşleşmesi durumunda döndüren true bir yöntemin temsilcisidir. Geçerli List<T> öğenin öğeleri tek tek temsilciye Predicate<T> geçirilir. Temsilcinin imzası vardır:

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

Bu yöntem doğrusal bir arama gerçekleştirir; bu nedenle, bu yöntem bir O(n) işlemidir; burada n olur Count.

Ayrıca bkz.

Şunlara uygulanır

FindIndex(Int32, Predicate<T>)

Source:
List.cs
Source:
List.cs
Source:
List.cs

Belirtilen koşul tarafından tanımlanan koşullarla eşleşen bir öğeyi arar ve öğesindeki belirtilen dizinden son öğeye kadar uzanan öğe List<T> aralığındaki ilk oluşumun sıfır tabanlı dizinini döndürür.

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

Parametreler

startIndex
Int32

Arama işleminin sıfır tabanlı başlangıç dizini.

match
Predicate<T>

Aranacak Predicate<T> öğenin koşullarını tanımlayan temsilci.

Döndürülenler

tarafından tanımlanan koşullarla matcheşleşen bir öğenin ilk oluşumunun sıfır tabanlı dizini; bulunursa, aksi takdirde -1.

Özel durumlar

match, null değeridir.

startIndex , için geçerli dizin aralığının dışındadır List<T>.

Örnekler

Aşağıdaki örnek, Name ve Idiki alanı olan bir Employee sınıfı tanımlar. Ayrıca, StartsWithalanın sınıf oluşturucusunun Employee.Name sağladığı EmployeeSearch belirtilen alt dizeyle başlayıp başlamadığını gösteren tek bir yöntemi olan bir sınıfı tanımlarEmployeeSearch. Bu yöntemin imzasını not edin

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

, yönteme geçirilebilen temsilcinin imzasına FindIndex karşılık gelir. Örnek bir List<Employee> nesneyi başlatır, nesneye bir dizi Employee nesne ekler ve ardından beşinci üyesinden (dizin 4'teki üye) başlayarak koleksiyonda arama yapmak için yöntemini iki kez çağırır FindIndex(Int32, Int32, Predicate<T>) . İlk kez, alanı "J" ile başlayan ilk Employee nesneyi Name arar; ikinci kez, alanı "Ju" ile başlayan ilk Employee nesneyi Name arar.

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

Açıklamalar

List<T> öğesinden başlayıp startIndex son öğeyle biten ileriye doğru arandı.

, Predicate<T> nesnesine geçirilen nesnenin temsilcide tanımlanan koşullarla eşleşmesi durumunda döndüren true bir yöntemin temsilcisidir. Geçerli List<T> öğenin öğeleri tek tek temsilciye Predicate<T> geçirilir. Temsilcinin imzası vardır:

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

Bu yöntem doğrusal bir arama gerçekleştirir; bu nedenle, bu yöntem bir O(n) işlemidir; burada n öğesinden startIndex sonuna kadar List<T>olan öğe sayısıdır.

Ayrıca bkz.

Şunlara uygulanır