Udostępnij za pośrednictwem


ArrayList.LastIndexOf Metoda

Definicja

Zwraca zerowy indeks ostatniego wystąpienia wartości w ArrayList części lub w niej.

Przeciążenia

LastIndexOf(Object)

Wyszukuje określony Object element i zwraca indeks zerowy ostatniego wystąpienia w całym ArrayListobiekcie .

LastIndexOf(Object, Int32)

Wyszukuje określony Object element i zwraca indeks zera ostatniego wystąpienia w zakresie elementów w ArrayList obiekcie, który rozciąga się od pierwszego elementu do określonego indeksu.

LastIndexOf(Object, Int32, Int32)

Wyszukuje określony Object element i zwraca indeks zera ostatniego wystąpienia w zakresie elementów w ArrayList obiekcie zawierającym określoną liczbę elementów i kończy się na określonym indeksie.

LastIndexOf(Object)

Źródło:
ArrayList.cs
Źródło:
ArrayList.cs
Źródło:
ArrayList.cs

Wyszukuje określony Object element i zwraca indeks zerowy ostatniego wystąpienia w całym ArrayListobiekcie .

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

Parametry

value
Object

Element Object do zlokalizowania w obiekcie ArrayList. Wartość może być null.

Zwraca

Indeks oparty na zerach ostatniego wystąpienia value w całym ArrayListobiekcie , jeśli zostanie znaleziony; w przeciwnym razie -1.

Przykłady

Poniższy przykład kodu pokazuje, jak określić indeks ostatniego wystąpienia określonego elementu.

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.

Uwagi

Element ArrayList jest wyszukiwany wstecz, zaczynając od ostatniego elementu i kończąc na pierwszym elemecie.

Ta metoda wykonuje wyszukiwanie liniowe; w związku z tym ta metoda jest operacją O(n) , gdzie n to Count.

Począwszy od .NET Framework 2.0, ta metoda używa metod i CompareTo obiektów Equals kolekcji do item określenia, czy element istnieje. We wcześniejszych wersjach .NET Framework ta determinacja została wykonana przy użyciu Equals metod item i CompareTo parametru na obiektach w kolekcji.

Zobacz też

Dotyczy

LastIndexOf(Object, Int32)

Źródło:
ArrayList.cs
Źródło:
ArrayList.cs
Źródło:
ArrayList.cs

Wyszukuje określony Object element i zwraca indeks zera ostatniego wystąpienia w zakresie elementów w ArrayList obiekcie, który rozciąga się od pierwszego elementu do określonego indeksu.

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

Parametry

value
Object

Element Object do zlokalizowania w obiekcie ArrayList. Wartość może być null.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

Zwraca

Indeks oparty na zerach ostatniego wystąpienia value w zakresie elementów w ArrayList obiekcie, który rozciąga się od pierwszego elementu do startIndex, jeśli zostanie znaleziony; w przeciwnym razie -1.

Wyjątki

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu ArrayList.

Przykłady

Poniższy przykład kodu pokazuje, jak określić indeks ostatniego wystąpienia określonego elementu.

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.

Uwagi

Element ArrayList jest wyszukiwany wstecz, zaczynając od startIndex i kończąc na pierwszym elemecie.

Ta metoda wykonuje wyszukiwanie liniowe; dlatego ta metoda jest operacją O(n) , gdzie n jest liczbą elementów od początku ArrayList do startIndex.

Ta metoda określa równość przez wywołanie metody Object.Equals.

Począwszy od .NET Framework 2.0, ta metoda używa metod i CompareTo obiektów Equals kolekcji do item określenia, czy element istnieje. We wcześniejszych wersjach .NET Framework ta determinacja została wykonana przy użyciu Equals metod item i CompareTo parametru na obiektach w kolekcji.

Zobacz też

Dotyczy

LastIndexOf(Object, Int32, Int32)

Źródło:
ArrayList.cs
Źródło:
ArrayList.cs
Źródło:
ArrayList.cs

Wyszukuje określony Object element i zwraca indeks zera ostatniego wystąpienia w zakresie elementów w ArrayList obiekcie zawierającym określoną liczbę elementów i kończy się na określonym indeksie.

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

Parametry

value
Object

Element Object do zlokalizowania w obiekcie ArrayList. Wartość może być null.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

count
Int32

Liczba elementów w sekcji do wyszukania.

Zwraca

Indeks oparty na zerach ostatniego wystąpienia value w zakresie elementów w ArrayList obiekcie zawierającym count liczbę elementów i kończy się na startIndexwartości , jeśli zostanie znaleziona; w przeciwnym razie -1.

Wyjątki

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu ArrayList.

-lub-

Parametr count ma wartość niższą niż zero.

-lub-

startIndex i count nie określają prawidłowej sekcji w pliku ArrayList.

Przykłady

Poniższy przykład kodu pokazuje, jak określić indeks ostatniego wystąpienia określonego elementu. Należy pamiętać, że LastIndexOf jest to wyszukiwanie wsteczne, count dlatego musi być mniejsze niż lub równe startIndex + 1.

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.

Uwagi

Element ArrayList jest wyszukiwany wstecz rozpoczynający się od i kończący startIndex się na startIndex minus count plus 1, jeśli count jest większy niż 0.

Ta metoda wykonuje wyszukiwanie liniowe; w związku z tym ta metoda jest operacją O(n) , gdzie n to count.

Ta metoda określa równość przez wywołanie metody Object.Equals.

Począwszy od .NET Framework 2.0, ta metoda używa metod i CompareTo obiektów Equals kolekcji do item określenia, czy element istnieje. We wcześniejszych wersjach .NET Framework ta determinacja została wykonana przy użyciu Equals metod item i CompareTo parametru na obiektach w kolekcji.

Zobacz też

Dotyczy