Sdílet prostřednictvím


ArrayList.LastIndexOf Metoda

Definice

Vrátí index od nuly posledního výskytu hodnoty v ArrayList nebo v části.

Přetížení

LastIndexOf(Object)

Vyhledá zadaný Object a vrátí index posledního výskytu v rámci celého ArrayListobjektu od nuly.

LastIndexOf(Object, Int32)

Vyhledá zadaný Object a vrátí index posledního výskytu založeného na nule v rozsahu prvků v objektu ArrayList , který se rozšiřuje od prvního prvku k zadanému indexu.

LastIndexOf(Object, Int32, Int32)

Vyhledá zadaný Object a vrátí index posledního výskytu od nuly v rozsahu prvků v objektu ArrayList , který obsahuje zadaný počet prvků a končí na zadaném indexu.

LastIndexOf(Object)

Zdroj:
ArrayList.cs
Zdroj:
ArrayList.cs
Zdroj:
ArrayList.cs

Vyhledá zadaný Object a vrátí index posledního výskytu v rámci celého ArrayListobjektu od nuly.

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

K Object vyhledání v ArrayList. Hodnota může být null.

Návraty

Nulový index posledního výskytu value v rámci celého objektu ArrayList, pokud je nalezen; jinak hodnota -1.

Příklady

Následující příklad kódu ukazuje, jak určit index posledního výskytu zadaného prvku.

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.

Poznámky

Prohledá ArrayList se zpět od posledního prvku a končící na prvním prvku.

Tato metoda provádí lineární vyhledávání; Proto je tato metoda operace O(n) , kde n je Count.

Počínaje rozhraním .NET Framework 2.0 tato metoda používá objekty Equals kolekce a CompareTo metody na item k určení, zda položka existuje. V dřívějších verzích rozhraní .NET Framework bylo toto určení provedeno pomocí Equals metod a CompareTo parametru item u objektů v kolekci.

Viz také

Platí pro

LastIndexOf(Object, Int32)

Zdroj:
ArrayList.cs
Zdroj:
ArrayList.cs
Zdroj:
ArrayList.cs

Vyhledá zadaný Object a vrátí index posledního výskytu založeného na nule v rozsahu prvků v objektu ArrayList , který se rozšiřuje od prvního prvku k zadanému indexu.

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

K Object vyhledání v ArrayList. Hodnota může být null.

startIndex
Int32

Počáteční index zpětného vyhledávání začínající od nuly.

Návraty

Index vycházející z nuly posledního výskytu value v rozsahu prvků v objektu ArrayList , který se rozšiřuje od prvního prvku do , pokud je startIndexnalezen; v opačném případě hodnota -1.

Výjimky

startIndex je mimo rozsah platných indexů pro ArrayList.

Příklady

Následující příklad kódu ukazuje, jak určit index posledního výskytu zadaného prvku.

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.

Poznámky

Prohledá ArrayList se zpět od startIndex prvního prvku a končící v prvním prvku.

Tato metoda provádí lineární vyhledávání; Proto je O(n) tato metoda operace, kde n je počet prvků od začátku ArrayList do startIndex.

Tato metoda určuje rovnost voláním Object.Equalsmetody .

Počínaje rozhraním .NET Framework 2.0 tato metoda používá objekty Equals kolekce a CompareTo metody na item k určení, zda položka existuje. V dřívějších verzích rozhraní .NET Framework bylo toto určení provedeno pomocí Equals metod a CompareTo parametru item u objektů v kolekci.

Viz také

Platí pro

LastIndexOf(Object, Int32, Int32)

Zdroj:
ArrayList.cs
Zdroj:
ArrayList.cs
Zdroj:
ArrayList.cs

Vyhledá zadaný Object a vrátí index posledního výskytu od nuly v rozsahu prvků v objektu ArrayList , který obsahuje zadaný počet prvků a končí na zadaném indexu.

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

K Object vyhledání v ArrayList. Hodnota může být null.

startIndex
Int32

Počáteční index zpětného vyhledávání začínající od nuly.

count
Int32

Počet prvků v prohledávané části.

Návraty

Index od nuly posledního value výskytu v rozsahu prvků v objektu ArrayList , který obsahuje count počet prvků a končí na startIndex, pokud je nalezen; jinak hodnota -1.

Výjimky

startIndex je mimo rozsah platných indexů pro ArrayList.

-nebo-

Hodnota count je menší než nula.

-nebo-

startIndexa count nezadávejte platný oddíl v .ArrayList

Příklady

Následující příklad kódu ukazuje, jak určit index posledního výskytu zadaného prvku. Všimněte si, že LastIndexOf hledání je zpětné. count Proto musí být menší než nebo rovno 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.

Poznámky

Prohledá ArrayList se zpět od a končící na startIndexstartIndex mínus count plus 1, pokud count je větší než 0.

Tato metoda provádí lineární vyhledávání; Proto je tato metoda operace O(n) , kde n je count.

Tato metoda určuje rovnost voláním Object.Equalsmetody .

Počínaje rozhraním .NET Framework 2.0 tato metoda používá objekty Equals kolekce a CompareTo metody na item k určení, zda položka existuje. V dřívějších verzích rozhraní .NET Framework bylo toto určení provedeno pomocí Equals metod a CompareTo parametru item u objektů v kolekci.

Viz také

Platí pro