C# ArrayList IndexOf(Object, Int32, Int32)
Description
ArrayList IndexOf(Object, Int32, Int32)
searches for
the specified Object and returns the zero-based index of the first occurrence
within the range of elements in the ArrayList that starts at the specified
index and contains the specified number of elements.
Syntax
ArrayList.IndexOf(Object, Int32, Int32)
has the following syntax.
public virtual int IndexOf(
Object value,// w w w.java 2s . c o m
int startIndex,
int count
)
Parameters
ArrayList.IndexOf(Object, Int32, 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 search. 0 (zero) is valid in an empty list.count
- The number of elements in the section to search.
Returns
ArrayList.IndexOf(Object, Int32, Int32)
method returns The zero-based index of the first occurrence of value within the range of
elements in the ArrayList that starts at startIndex and contains count number
of elements, if found; otherwise, -1.
Example
The following code shows how to
search for the first occurrence of the
duplicated value in a section of the ArrayList
.
using System;//from w w w . j av a 2 s.co m
using System.Collections;
public class SamplesArrayList
{
public static void Main()
{
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" );
String myString = "the";
int myIndex = -1;
myIndex = myAL.IndexOf( myString, 6, 6 );
Console.WriteLine( myIndex );
}
public static void PrintIndexAndValues(IEnumerable myList)
{
int i = 0;
foreach (Object obj in myList)
Console.WriteLine(" [{0}]: {1}", i++, obj);
}
}
The code above generates the following result.