Export (0) Print
Expand All

ArrayList.IndexOf Method

Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.

Overload List

Searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function IndexOf(Object) As Integer Implements IList.IndexOf
[C#] public virtual int IndexOf(object);
[C++] public: virtual int IndexOf(Object*);
[JScript] public function IndexOf(Object) : int;

Searches for the specified Object and returns the zero-based index of the first occurrence within the section of the ArrayList that extends from the specified index to the last element.

[Visual Basic] Overloads Public Overridable Function IndexOf(Object, Integer) As Integer
[C#] public virtual int IndexOf(object, int);
[C++] public: virtual int IndexOf(Object*, int);
[JScript] public function IndexOf(Object, int) : int;

Searches for the specified Object and returns the zero-based index of the first occurrence within the section of the ArrayList that starts at the specified index and contains the specified number of elements.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function IndexOf(Object, Integer, Integer) As Integer
[C#] public virtual int IndexOf(object, int, int);
[C++] public: virtual int IndexOf(Object*, int, int);
[JScript] public function IndexOf(Object, int, int) : int;

Example

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

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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)
      
      ' Search for the first occurrence of the duplicated value.
      Dim myString As [String] = "the"
      Dim myIndex As Integer = myAL.IndexOf(myString)
      Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.", myString, myIndex)
      
      ' Search for the first occurrence of the duplicated value in the last section of the ArrayList.
      myIndex = myAL.IndexOf(myString, 4)
      Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.", myString, myIndex)
      
      ' Search for the first occurrence of the duplicated value in a section of the ArrayList.
      myIndex = myAL.IndexOf(myString, 6, 6)
      Console.WriteLine("The first occurrence of ""{0}"" between index 6 and index 11 is at index {1}.", myString, myIndex)
   End Sub 'Main
   
   
   Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
      Dim i As Integer = 0
      Dim myEnumerator As System.Collections.IEnumerator = myList.GetEnumerator()
      While myEnumerator.MoveNext()
         Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab + "{1}", i, myEnumerator.Current)
         i = i + 1
      End While
      Console.WriteLine()
   End Sub 'PrintIndexAndValues
End Class '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 first occurrence of "the" is at index 0.
' The first occurrence of "the" between index 4 and the end is at index 6.
' The first occurrence of "the" between index 6 and index 11 is at index 6.
' 

[C#] 
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 );

      // Search for the first occurrence of the duplicated value.
      String myString = "the";
      int myIndex = myAL.IndexOf( myString );
      Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

      // Search for the first occurrence of the duplicated value in the last section of the ArrayList.
      myIndex = myAL.IndexOf( myString, 4 );
      Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );

      // Search for the first occurrence of the duplicated value in a section of the ArrayList.
      myIndex = myAL.IndexOf( myString, 6, 6 );
      Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 is at index {1}.", myString, myIndex );
   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      System.Collections.IEnumerator myEnumerator = 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]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
    [9]:    in
    [10]:    the
    [11]:    barn

The first occurrence of "the" is at index 0.
The first occurrence of "the" between index 4 and the end is at index 6.
The first occurrence of "the" between index 6 and index 11 is at index 6.
*/ 

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void PrintIndexAndValues( IEnumerable* myList );
 
void main()  {
 
       // Creates and initializes a new ArrayList instance with three elements of the same value.
       ArrayList* myAL = new ArrayList();
       myAL->Add( S"the" );
       myAL->Add( S"quick" );
       myAL->Add( S"brown" );
       myAL->Add( S"fox" );
       myAL->Add( S"jumped" );
       myAL->Add( S"over" );
       myAL->Add( S"the" );
       myAL->Add( S"lazy" );
       myAL->Add( S"dog" );
       myAL->Add( S"in" );
       myAL->Add( S"the" );
       myAL->Add( S"barn" );
 
       // Displays the values of the ArrayList.
       Console::WriteLine( "The ArrayList instance contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Searches for the first occurrence of the duplicated value.
       String* myString = "the";
       int myIndex = myAL->IndexOf( myString );
       Console::WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, __box(myIndex) );
 
       // Searches for the first occurrence of the duplicated value in the last section of the ArrayList.
       myIndex = myAL->IndexOf( myString, 4 );
       Console::WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, __box(myIndex) );
 
       // Searches for the first occurrence of the duplicated value in a section of the ArrayList.
       myIndex = myAL->IndexOf( myString, 6, 6 );
       Console::WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 is at index {1}.", myString, __box(myIndex) );
    }
 
void PrintIndexAndValues( IEnumerable* myList )  {
       int i = 0;
       System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
       while ( myEnumerator->MoveNext() )
          Console::WriteLine( "\t[{0}]:\t{1}", __box(i++), myEnumerator->Current );
       Console::WriteLine();
    }

 /* 
 This code produces the following output.
 
 The ArrayList instance 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 first occurrence of "the" is at index 0.
 The first occurrence of "the" between index 4 and the end is at index 6.
 The first occurrence of "the" between index 6 and index 11 is at index 6.
 */ 

[JScript] 
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 );

// Search for the first occurrence of the duplicated value.
var myString : String = "the";
var myIndex : int = myAL.IndexOf( myString );
Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Search for the first occurrence of the duplicated value in the last section of the ArrayList.
myIndex = myAL.IndexOf( myString, 4 );
Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );

// Search for the first occurrence of the duplicated value in a section of the ArrayList.
myIndex = myAL.IndexOf( myString, 6, 6 );
Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 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 first occurrence of "the" is at index 0.
 The first occurrence of "the" between index 4 and the end is at index 6.
 The first occurrence of "the" between index 6 and index 11 is at index 6.
 */ 

See Also

ArrayList Class | ArrayList Members | System.Collections Namespace

Show:
© 2014 Microsoft