ArrayList.LastIndexOf Methode

Definition

Gibt den nullbasierten Index des letzten Vorkommens eines Werts in der ArrayList bzw. in einem Abschnitt davon zurück.

Überlädt

LastIndexOf(Object)

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb der gesamten ArrayList zurück.

LastIndexOf(Object, Int32)

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der sich vom ersten Element bis zum angegebenen Index erstreckt.

LastIndexOf(Object, Int32, Int32)

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der die angegebene Anzahl von Elementen enthält und am angegebenen Index endet.

LastIndexOf(Object)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb der gesamten ArrayList zurück.

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

Parameter

value
Object

Das Object, das in der ArrayList gesucht werden soll. Der Wert kann null sein.

Gibt zurück

Der nullbasierte Index des letzten Vorkommens von value in der gesamten ArrayList; andernfalls –1.

Beispiele

Das folgende Codebeispiel zeigt, wie Der Index des letzten Vorkommens eines angegebenen Elements bestimmt wird.

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.

Hinweise

Das ArrayList wird rückwärts gesucht, beginnend mit dem letzten Element und endend mit dem ersten Element.

Diese Methode führt eine lineare Suche durch. daher ist diese Methode ein O(n) Vorgang, wobei n ist Count.

Ab dem .NET Framework 2.0 verwendet diese Methode die -Objekte Equals und CompareTo -Methoden item der Auflistung, um zu bestimmen, ob ein Element vorhanden ist. In den früheren Versionen des .NET Framework wurde diese Bestimmung mithilfe der Equals Methoden und CompareTo des item Parameters für die Objekte in der Auflistung vorgenommen.

Weitere Informationen

Gilt für:

LastIndexOf(Object, Int32)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der sich vom ersten Element bis zum angegebenen Index erstreckt.

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

Parameter

value
Object

Das Object, das in der ArrayList gesucht werden soll. Der Wert kann null sein.

startIndex
Int32

Der nullbasierte Startindex für die Rückwärtssuche.

Gibt zurück

Der nullbasierte Index des letzten Vorkommens von value innerhalb des Bereichs von Elementen in der ArrayList, der sich vom ersten Element bis startIndex erstreckt, sofern gefunden; andernfalls –1.

Ausnahmen

startIndex liegt außerhalb des Bereichs der gültigen Indizes für das ArrayList.

Beispiele

Das folgende Codebeispiel zeigt, wie Der Index des letzten Vorkommens eines angegebenen Elements bestimmt wird.

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.

Hinweise

Der ArrayList wird rückwärts gesucht, beginnend am startIndex und endend beim ersten Element.

Diese Methode führt eine lineare Suche durch. Daher ist diese Methode ein O(n) Vorgang, bei dem n die Anzahl der Elemente vom Anfang von ArrayList bis ist startIndex.

Diese Methode bestimmt die Gleichheit durch Aufrufen Object.Equalsvon .

Ab dem .NET Framework 2.0 verwendet diese Methode die -Objekte Equals und CompareTo -Methoden item der Auflistung, um zu bestimmen, ob ein Element vorhanden ist. In den früheren Versionen des .NET Framework wurde diese Bestimmung mithilfe der Equals Methoden und CompareTo des item Parameters für die Objekte in der Auflistung vorgenommen.

Weitere Informationen

Gilt für:

LastIndexOf(Object, Int32, Int32)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der die angegebene Anzahl von Elementen enthält und am angegebenen Index endet.

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

Parameter

value
Object

Das Object, das in der ArrayList gesucht werden soll. Der Wert kann null sein.

startIndex
Int32

Der nullbasierte Startindex für die Rückwärtssuche.

count
Int32

Die Anzahl der Elemente im zu durchsuchenden Abschnitt.

Gibt zurück

Der nullbasierte Index des letzten Vorkommens von value innerhalb des Bereichs von Elementen in der ArrayList, der count Anzahl von Elementen enthält und am startIndex endet, sofern gefunden; andernfalls –1.

Ausnahmen

startIndex liegt außerhalb des Bereichs der gültigen Indizes für das ArrayList.

- oder -

count ist kleiner als Null.

- oder -

startIndex und count geben keinen gültigen Abschnitt in der ArrayList an.

Beispiele

Das folgende Codebeispiel zeigt, wie Der Index des letzten Vorkommens eines angegebenen Elements bestimmt wird. Beachten Sie, dass LastIndexOf es sich um eine Rückwärtssuche handelt. count Daher muss kleiner oder gleich startIndex + 1 sein.

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.

Hinweise

Die ArrayList wird rückwärts gesucht, beginnend bei und endet bei startIndexstartIndex minus count plus 1, wenn count größer als 0 ist.

Diese Methode führt eine lineare Suche durch. daher ist diese Methode ein O(n) Vorgang, wobei n ist count.

Diese Methode bestimmt die Gleichheit durch Aufrufen Object.Equalsvon .

Ab dem .NET Framework 2.0 verwendet diese Methode die -Objekte Equals und CompareTo -Methoden item der Auflistung, um zu bestimmen, ob ein Element vorhanden ist. In den früheren Versionen des .NET Framework wurde diese Bestimmung mithilfe der Equals Methoden und CompareTo des item Parameters für die Objekte in der Auflistung vorgenommen.

Weitere Informationen

Gilt für: