Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ArrayList.LastIndexOf Method (Object, Int32, 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 contains the specified number of elements and ends at the specified index.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public virtual int LastIndexOf (
	Object value,
	int startIndex,
	int count
)
public int LastIndexOf (
	Object value, 
	int startIndex, 
	int count
)
public function LastIndexOf (
	value : Object, 
	startIndex : int, 
	count : int
) : int
Not applicable.

Parameters

value

The Object to locate in the ArrayList. The value can be a null reference (Nothing in Visual Basic).

startIndex

The zero-based starting index of the backward search.

count

The number of elements in the section to search.

Return Value

The zero-based index of the last occurrence of value within the range of elements in the ArrayList that contains count number of elements and ends at startIndex, if found; otherwise, -1.

Exception typeCondition

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for the ArrayList.

-or-

count is less than zero.

-or-

startIndex and count do not specify a valid section in the ArrayList.

The ArrayList is searched backward starting at startIndex and ending at startIndex minus count plus 1, if count is greater than 0.

This method performs a linear search; therefore, this method is an O(n) operation, where n is count.

This method determines equality by calling Object.Equals.

Starting with the .NET Framework 2.0, this method uses the collection’s objects’ Equals and CompareTo methods on item to determine whether item exists. In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item parameter on the objects in the collection.

The following code example shows how to determine the index of the last occurrence of a specified element. Note that LastIndexOf is a backward search; therefore, count must be less than or equal to startIndex + 1.

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.
*/ 

import System.*;
import System.Collections.*;

public class SamplesArrayList
{
    public static void main(String[] args)
    {
        // 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,(Int32)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, (Int32)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, (Int32)myIndex);
    } //main

    public static void PrintIndexAndValues(IEnumerable myList)
    {
        int i = 0;
        IEnumerator objMyEnum = myList.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            Console.WriteLine("   [{0}]:    {1}", (Int32)i++, obj);
        }
        Console.WriteLine();
    } //PrintIndexAndValues
} //SamplesArrayList 
/*
 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.
 */

import System;
import System.Collections;

// Creates and initializes a new ArrayList with three elements of the same value.
var myAL : ArrayList = new ArrayList();
myAL.Add( "the" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
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.
var myString : String = "the";
var myIndex : int = 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 );
 
function PrintIndexAndValues( myList : IEnumerable )  {
   var i : int = 0;
   var myEnumerator : System.Collections.IEnumerator  = myList.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
   Console.WriteLine();
}
 /*
 This code produces the following output.
 
 The ArrayList contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [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.
 */ 

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft