Aracılığıyla paylaş


ArrayList.LastIndexOf Yöntem

Tanım

Bir değerin ArrayList içindeki veya içindeki son oluşumunun sıfır tabanlı dizinini döndürür.

Aşırı Yüklemeler

LastIndexOf(Object)

Belirtilen Object öğesini arar ve içindeki ArrayListson oluşumun sıfır tabanlı dizinini döndürür.

LastIndexOf(Object, Int32)

Belirtilen Object öğesini arar ve öğesindeki ilk öğeden belirtilen dizine kadar uzanan öğe aralığındaki ArrayList son oluşumun sıfır tabanlı dizinini döndürür.

LastIndexOf(Object, Int32, Int32)

Belirtilen öğesini arar ve öğesinde belirtilen Object sayıda öğe içeren ve belirtilen dizinde ArrayList biten öğe aralığındaki son oluşumun sıfır tabanlı dizinini döndürür.

LastIndexOf(Object)

Kaynak:
ArrayList.cs
Kaynak:
ArrayList.cs
Kaynak:
ArrayList.cs

Belirtilen Object öğesini arar ve içindeki ArrayListson oluşumun sıfır tabanlı dizinini döndürür.

public:
 virtual int LastIndexOf(System::Object ^ value);
public virtual int LastIndexOf (object value);
public virtual int LastIndexOf (object? value);
abstract member LastIndexOf : obj -> int
override this.LastIndexOf : obj -> int
Public Overridable Function LastIndexOf (value As Object) As Integer

Parametreler

value
Object

Object içinde bulunacak.ArrayList Değer olabilir null.

Döndürülenler

tüm içinde ArrayListöğesinin son oluşumunun value sıfır tabanlı dizini; bulunursa, aksi takdirde -1.

Örnekler

Aşağıdaki kod örneğinde, belirtilen öğenin son oluşumunun dizininin nasıl belirleneceği gösterilmektedir.

using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList with three elements of the same value.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "the" );
   myAL->Add( "quick" );
   myAL->Add( "brown" );
   myAL->Add( "fox" );
   myAL->Add( "jumps" );
   myAL->Add( "over" );
   myAL->Add( "the" );
   myAL->Add( "lazy" );
   myAL->Add( "dog" );
   myAL->Add( "in" );
   myAL->Add( "the" );
   myAL->Add( "barn" );
   
   // Displays the values of the ArrayList.
   Console::WriteLine( "The ArrayList contains the following values:" );
   PrintIndexAndValues( myAL );
   
   // Searches for the last occurrence of the duplicated value.
   String^ myString = "the";
   int myIndex = myAL->LastIndexOf( myString );
   Console::WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
   
   // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
   myIndex = myAL->LastIndexOf( myString, 8 );
   Console::WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
   
   // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
   myIndex = myAL->LastIndexOf( myString, 10, 6 );
   Console::WriteLine( "The last occurrence of \"{0}\" between index 10 and index 5 is at index {1}.", myString, myIndex );
}

void PrintIndexAndValues( IEnumerable^ myList )
{
   int i = 0;
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   [{0}]:    {1}", i++, obj );
   }

   Console::WriteLine();
}

/*
 This code produces the following output.
 
 The ArrayList contains the following values:
    [0]:    the
    [1]:    quick
    [2]:    brown
    [3]:    fox
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
    [9]:    in
    [10]:    the
    [11]:    barn

 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 10 and index 5 is at index 10.
 */
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList with three elements of the same value.
      ArrayList myAL = new ArrayList();
      myAL.Add( "the" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );
      myAL.Add( "in" );
      myAL.Add( "the" );
      myAL.Add( "barn" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList contains the following values:" );
      PrintIndexAndValues( myAL );

      // Searches for the last occurrence of the duplicated value.
      string myString = "the";
      int myIndex = myAL.LastIndexOf( myString );
      Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

      // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
      myIndex = myAL.LastIndexOf( myString, 8 );
      Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

      // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
      myIndex = myAL.LastIndexOf( myString, 10, 6 );
      Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 5 is at index {1}.", myString, myIndex );
   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      foreach ( Object obj in myList )
         Console.WriteLine( "   [{0}]:    {1}", i++, obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

The ArrayList contains the following values:
   [0]:    the
   [1]:    quick
   [2]:    brown
   [3]:    fox
   [4]:    jumps
   [5]:    over
   [6]:    the
   [7]:    lazy
   [8]:    dog
   [9]:    in
   [10]:    the
   [11]:    barn

The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 10 and index 5 is at index 10.
*/
Imports System.Collections

Public Class SamplesArrayList
   
   Public Shared Sub Main()
      
      ' Creates and initializes a new ArrayList with three elements of the same value.
      Dim myAL As New ArrayList()
      myAL.Add("the")
      myAL.Add("quick")
      myAL.Add("brown")
      myAL.Add("fox")
      myAL.Add("jumps")
      myAL.Add("over")
      myAL.Add("the")
      myAL.Add("lazy")
      myAL.Add("dog")
      myAL.Add("in")
      myAL.Add("the")
      myAL.Add("barn")
      
      ' Displays the values of the ArrayList.
      Console.WriteLine("The ArrayList contains the following values:")
      PrintIndexAndValues(myAL)
      
      ' Searches for the last occurrence of the duplicated value.
      Dim myString As [String] = "the"
      Dim myIndex As Integer = myAL.LastIndexOf(myString)
      Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", myString, myIndex)
      
      ' Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
      myIndex = myAL.LastIndexOf(myString, 8)
      Console.WriteLine("The last occurrence of ""{0}"" between the start and index 8 is at index {1}.", myString, myIndex)
      
      ' Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
      myIndex = myAL.LastIndexOf(myString, 10, 6)
      Console.WriteLine("The last occurrence of ""{0}"" between index 10 and index 5 is at index {1}.", myString, myIndex)
   End Sub
   
   
   Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
      Dim i as Integer
      Dim obj As [Object]
      For Each obj In  myList
         Console.WriteLine("   [{0}]:    {1}", i, obj)
         i = i + 1
      Next obj
      Console.WriteLine()
   End Sub

End Class

' This code produces the following output.
'
' The ArrayList contains the following values:
'    [0]:    the
'    [1]:    quick
'    [2]:    brown
'    [3]:    fox
'    [4]:    jumps
'    [5]:    over
'    [6]:    the
'    [7]:    lazy
'    [8]:    dog
'    [9]:    in
'    [10]:    the
'    [11]:    barn
'
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 10 and index 5 is at index 10.

Açıklamalar

, ArrayList son öğeden başlayıp ilk öğeyle biten geriye doğru arandı.

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

.NET Framework 2.0'dan başlayarak, bu yöntem öğenin mevcut olup olmadığını belirlemek için koleksiyonunun nesnelerini Equals ve CompareTo yöntemlerini item kullanır. .NET Framework önceki sürümlerinde bu belirleme, koleksiyondaki Equals nesneler üzerinde parametresinin item ve CompareTo yöntemleri kullanılarak yapılmıştır.

Ayrıca bkz.

Şunlara uygulanır

LastIndexOf(Object, Int32)

Kaynak:
ArrayList.cs
Kaynak:
ArrayList.cs
Kaynak:
ArrayList.cs

Belirtilen Object öğesini arar ve öğesindeki ilk öğeden belirtilen dizine kadar uzanan öğe aralığındaki ArrayList son oluşumun sıfır tabanlı dizinini döndürür.

public:
 virtual int LastIndexOf(System::Object ^ value, int startIndex);
public virtual int LastIndexOf (object value, int startIndex);
public virtual int LastIndexOf (object? value, int startIndex);
abstract member LastIndexOf : obj * int -> int
override this.LastIndexOf : obj * int -> int
Public Overridable Function LastIndexOf (value As Object, startIndex As Integer) As Integer

Parametreler

value
Object

Object içinde bulunacak.ArrayList Değer olabilir null.

startIndex
Int32

Geriye doğru arama işleminin sıfır tabanlı başlangıç dizini.

Döndürülenler

içindeki öğeleri ArrayList aralığındaki son oluşumunun value sıfır tabanlı dizini, ilk öğeden startIndexbulunursa öğesine kadar uzanır; aksi takdirde -1.

Özel durumlar

startIndex , için geçerli dizin aralığının dışındadır ArrayList.

Örnekler

Aşağıdaki kod örneğinde, belirtilen öğenin son oluşumunun dizininin nasıl belirleneceği gösterilmektedir.

using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList with three elements of the same value.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "the" );
   myAL->Add( "quick" );
   myAL->Add( "brown" );
   myAL->Add( "fox" );
   myAL->Add( "jumps" );
   myAL->Add( "over" );
   myAL->Add( "the" );
   myAL->Add( "lazy" );
   myAL->Add( "dog" );
   myAL->Add( "in" );
   myAL->Add( "the" );
   myAL->Add( "barn" );
   
   // Displays the values of the ArrayList.
   Console::WriteLine( "The ArrayList contains the following values:" );
   PrintIndexAndValues( myAL );
   
   // Searches for the last occurrence of the duplicated value.
   String^ myString = "the";
   int myIndex = myAL->LastIndexOf( myString );
   Console::WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
   
   // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
   myIndex = myAL->LastIndexOf( myString, 8 );
   Console::WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
   
   // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
   myIndex = myAL->LastIndexOf( myString, 10, 6 );
   Console::WriteLine( "The last occurrence of \"{0}\" between index 10 and index 5 is at index {1}.", myString, myIndex );
}

void PrintIndexAndValues( IEnumerable^ myList )
{
   int i = 0;
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   [{0}]:    {1}", i++, obj );
   }

   Console::WriteLine();
}

/*
 This code produces the following output.
 
 The ArrayList contains the following values:
    [0]:    the
    [1]:    quick
    [2]:    brown
    [3]:    fox
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
    [9]:    in
    [10]:    the
    [11]:    barn

 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 10 and index 5 is at index 10.
 */
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList with three elements of the same value.
      ArrayList myAL = new ArrayList();
      myAL.Add( "the" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );
      myAL.Add( "in" );
      myAL.Add( "the" );
      myAL.Add( "barn" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList contains the following values:" );
      PrintIndexAndValues( myAL );

      // Searches for the last occurrence of the duplicated value.
      string myString = "the";
      int myIndex = myAL.LastIndexOf( myString );
      Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

      // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
      myIndex = myAL.LastIndexOf( myString, 8 );
      Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

      // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
      myIndex = myAL.LastIndexOf( myString, 10, 6 );
      Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 5 is at index {1}.", myString, myIndex );
   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      foreach ( Object obj in myList )
         Console.WriteLine( "   [{0}]:    {1}", i++, obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

The ArrayList contains the following values:
   [0]:    the
   [1]:    quick
   [2]:    brown
   [3]:    fox
   [4]:    jumps
   [5]:    over
   [6]:    the
   [7]:    lazy
   [8]:    dog
   [9]:    in
   [10]:    the
   [11]:    barn

The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 10 and index 5 is at index 10.
*/
Imports System.Collections

Public Class SamplesArrayList
   
   Public Shared Sub Main()
      
      ' Creates and initializes a new ArrayList with three elements of the same value.
      Dim myAL As New ArrayList()
      myAL.Add("the")
      myAL.Add("quick")
      myAL.Add("brown")
      myAL.Add("fox")
      myAL.Add("jumps")
      myAL.Add("over")
      myAL.Add("the")
      myAL.Add("lazy")
      myAL.Add("dog")
      myAL.Add("in")
      myAL.Add("the")
      myAL.Add("barn")
      
      ' Displays the values of the ArrayList.
      Console.WriteLine("The ArrayList contains the following values:")
      PrintIndexAndValues(myAL)
      
      ' Searches for the last occurrence of the duplicated value.
      Dim myString As [String] = "the"
      Dim myIndex As Integer = myAL.LastIndexOf(myString)
      Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", myString, myIndex)
      
      ' Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
      myIndex = myAL.LastIndexOf(myString, 8)
      Console.WriteLine("The last occurrence of ""{0}"" between the start and index 8 is at index {1}.", myString, myIndex)
      
      ' Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
      myIndex = myAL.LastIndexOf(myString, 10, 6)
      Console.WriteLine("The last occurrence of ""{0}"" between index 10 and index 5 is at index {1}.", myString, myIndex)
   End Sub
   
   
   Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
      Dim i as Integer
      Dim obj As [Object]
      For Each obj In  myList
         Console.WriteLine("   [{0}]:    {1}", i, obj)
         i = i + 1
      Next obj
      Console.WriteLine()
   End Sub

End Class

' This code produces the following output.
'
' The ArrayList contains the following values:
'    [0]:    the
'    [1]:    quick
'    [2]:    brown
'    [3]:    fox
'    [4]:    jumps
'    [5]:    over
'    [6]:    the
'    [7]:    lazy
'    [8]:    dog
'    [9]:    in
'    [10]:    the
'    [11]:    barn
'
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 10 and index 5 is at index 10.

Açıklamalar

ArrayList öğesinden başlayıp startIndex ilk öğeyle biten geriye doğru arandı.

Bu yöntem doğrusal bir arama gerçekleştirir; bu nedenle, bu yöntem bir işlemdir O(n) ; burada n öğesinin başından öğesine ArrayListstartIndexkadar öğelerin sayısıdır.

Bu yöntem çağırarak Object.Equalseşitliği belirler.

.NET Framework 2.0'dan başlayarak, bu yöntem öğenin mevcut olup olmadığını belirlemek için koleksiyonunun nesnelerini Equals ve CompareTo yöntemlerini item kullanır. .NET Framework önceki sürümlerinde bu belirleme, koleksiyondaki Equals nesneler üzerinde parametresinin item ve CompareTo yöntemleri kullanılarak yapılmıştır.

Ayrıca bkz.

Şunlara uygulanır

LastIndexOf(Object, Int32, Int32)

Kaynak:
ArrayList.cs
Kaynak:
ArrayList.cs
Kaynak:
ArrayList.cs

Belirtilen öğesini arar ve öğesinde belirtilen Object sayıda öğe içeren ve belirtilen dizinde ArrayList biten öğe aralığındaki son oluşumun sıfır tabanlı dizinini döndürür.

public:
 virtual int LastIndexOf(System::Object ^ value, int startIndex, int count);
public virtual int LastIndexOf (object value, int startIndex, int count);
public virtual int LastIndexOf (object? value, int startIndex, int count);
abstract member LastIndexOf : obj * int * int -> int
override this.LastIndexOf : obj * int * int -> int
Public Overridable Function LastIndexOf (value As Object, startIndex As Integer, count As Integer) As Integer

Parametreler

value
Object

Object içinde bulunacak.ArrayList Değer olabilir null.

startIndex
Int32

Geriye doğru arama işleminin sıfır tabanlı başlangıç dizini.

count
Int32

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

Döndürülenler

içinde öğe sayısı içeren count ve bulunursa konumunda biten startIndexöğe ArrayList aralığındaki son oluşumunun value sıfır tabanlı dizini; aksi takdirde -1.

Özel durumlar

startIndex , için geçerli dizin aralığının dışındadır ArrayList.

-veya-

count, sıfırdan küçüktür.

-veya-

startIndex ve count içinde ArrayListgeçerli bir bölüm belirtmeyin.

Örnekler

Aşağıdaki kod örneğinde, belirtilen öğenin son oluşumunun dizininin nasıl belirleneceği gösterilmektedir. Geriye dönük bir arama olduğunu LastIndexOf unutmayın; bu nedenle + count 1'den startIndex küçük veya buna eşit olmalıdır.

using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList with three elements of the same value.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "the" );
   myAL->Add( "quick" );
   myAL->Add( "brown" );
   myAL->Add( "fox" );
   myAL->Add( "jumps" );
   myAL->Add( "over" );
   myAL->Add( "the" );
   myAL->Add( "lazy" );
   myAL->Add( "dog" );
   myAL->Add( "in" );
   myAL->Add( "the" );
   myAL->Add( "barn" );
   
   // Displays the values of the ArrayList.
   Console::WriteLine( "The ArrayList contains the following values:" );
   PrintIndexAndValues( myAL );
   
   // Searches for the last occurrence of the duplicated value.
   String^ myString = "the";
   int myIndex = myAL->LastIndexOf( myString );
   Console::WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
   
   // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
   myIndex = myAL->LastIndexOf( myString, 8 );
   Console::WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
   
   // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
   myIndex = myAL->LastIndexOf( myString, 10, 6 );
   Console::WriteLine( "The last occurrence of \"{0}\" between index 10 and index 5 is at index {1}.", myString, myIndex );
}

void PrintIndexAndValues( IEnumerable^ myList )
{
   int i = 0;
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   [{0}]:    {1}", i++, obj );
   }

   Console::WriteLine();
}

/*
 This code produces the following output.
 
 The ArrayList contains the following values:
    [0]:    the
    [1]:    quick
    [2]:    brown
    [3]:    fox
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
    [9]:    in
    [10]:    the
    [11]:    barn

 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 10 and index 5 is at index 10.
 */
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList with three elements of the same value.
      ArrayList myAL = new ArrayList();
      myAL.Add( "the" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );
      myAL.Add( "in" );
      myAL.Add( "the" );
      myAL.Add( "barn" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList contains the following values:" );
      PrintIndexAndValues( myAL );

      // Searches for the last occurrence of the duplicated value.
      string myString = "the";
      int myIndex = myAL.LastIndexOf( myString );
      Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

      // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
      myIndex = myAL.LastIndexOf( myString, 8 );
      Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

      // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
      myIndex = myAL.LastIndexOf( myString, 10, 6 );
      Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 5 is at index {1}.", myString, myIndex );
   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      foreach ( Object obj in myList )
         Console.WriteLine( "   [{0}]:    {1}", i++, obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

The ArrayList contains the following values:
   [0]:    the
   [1]:    quick
   [2]:    brown
   [3]:    fox
   [4]:    jumps
   [5]:    over
   [6]:    the
   [7]:    lazy
   [8]:    dog
   [9]:    in
   [10]:    the
   [11]:    barn

The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 10 and index 5 is at index 10.
*/
Imports System.Collections

Public Class SamplesArrayList
   
   Public Shared Sub Main()
      
      ' Creates and initializes a new ArrayList with three elements of the same value.
      Dim myAL As New ArrayList()
      myAL.Add("the")
      myAL.Add("quick")
      myAL.Add("brown")
      myAL.Add("fox")
      myAL.Add("jumps")
      myAL.Add("over")
      myAL.Add("the")
      myAL.Add("lazy")
      myAL.Add("dog")
      myAL.Add("in")
      myAL.Add("the")
      myAL.Add("barn")
      
      ' Displays the values of the ArrayList.
      Console.WriteLine("The ArrayList contains the following values:")
      PrintIndexAndValues(myAL)
      
      ' Searches for the last occurrence of the duplicated value.
      Dim myString As [String] = "the"
      Dim myIndex As Integer = myAL.LastIndexOf(myString)
      Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", myString, myIndex)
      
      ' Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
      myIndex = myAL.LastIndexOf(myString, 8)
      Console.WriteLine("The last occurrence of ""{0}"" between the start and index 8 is at index {1}.", myString, myIndex)
      
      ' Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
      myIndex = myAL.LastIndexOf(myString, 10, 6)
      Console.WriteLine("The last occurrence of ""{0}"" between index 10 and index 5 is at index {1}.", myString, myIndex)
   End Sub
   
   
   Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
      Dim i as Integer
      Dim obj As [Object]
      For Each obj In  myList
         Console.WriteLine("   [{0}]:    {1}", i, obj)
         i = i + 1
      Next obj
      Console.WriteLine()
   End Sub

End Class

' This code produces the following output.
'
' The ArrayList contains the following values:
'    [0]:    the
'    [1]:    quick
'    [2]:    brown
'    [3]:    fox
'    [4]:    jumps
'    [5]:    over
'    [6]:    the
'    [7]:    lazy
'    [8]:    dog
'    [9]:    in
'    [10]:    the
'    [11]:    barn
'
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 10 and index 5 is at index 10.

Açıklamalar

ArrayList, 0'dan startIndex büyükse count eksi count artı 1 ile startIndex başlayıp 1 ile biten geriye doğru arama yapılır.

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

Bu yöntem çağırarak Object.Equalseşitliği belirler.

.NET Framework 2.0'dan başlayarak, bu yöntem öğenin mevcut olup olmadığını belirlemek için koleksiyonunun nesnelerini Equals ve CompareTo yöntemlerini item kullanır. .NET Framework önceki sürümlerinde bu belirleme, koleksiyondaki Equals nesneler üzerinde parametresinin item ve CompareTo yöntemleri kullanılarak yapılmıştır.

Ayrıca bkz.

Şunlara uygulanır