C# ArrayList LastIndexOf(Object, Int32)

Description

ArrayList LastIndexOf(Object, Int32) searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that extends from the first element to the specified index.

Syntax

ArrayList.LastIndexOf(Object, Int32) has the following syntax.


public virtual int LastIndexOf(
  Object value,
  int startIndex
)

Parameters

ArrayList.LastIndexOf(Object, Int32) has the following parameters.

  • value - The Object to locate in the ArrayList. The value can be null.
  • startIndex - The zero-based starting index of the backward search.

Returns

ArrayList.LastIndexOf(Object, Int32) method returns The zero-based index of the last occurrence of value within the range of elements in the ArrayList that extends from the first element to startIndex, if found; otherwise, -1.

Example

The following code example shows how to determine the index of the last occurrence of a specified element.


using System;//from w  w  w.  ja v  a  2  s .  co  m
using System.Collections;
public class SamplesArrayList  {
   public static void Main()  {
      ArrayList myAL = new ArrayList();
      myAL.Add( "A" );
      myAL.Add( "B" );
      myAL.Add( "C" );
      myAL.Add( "D" );
      myAL.Add( "E" );
      myAL.Add( "F" );
      myAL.Add( "G" );
      myAL.Add( "H" );
      myAL.Add( "I" );
      myAL.Add( "J" );
      myAL.Add( "K" );
      myAL.Add( "L" );

      String myString = "G";
      int myIndex = myAL.LastIndexOf( myString );
      Console.WriteLine(myIndex );

      myIndex = myAL.LastIndexOf( myString, 8 );
      Console.WriteLine(myIndex );

      myIndex = myAL.LastIndexOf( myString, 10, 6 );
      Console.WriteLine( myIndex );
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack