ArrayList.IndexOf Método

Definición

Devuelve el índice de base cero de la primera aparición de un valor en ArrayList o en una parte.

Sobrecargas

IndexOf(Object)

Busca el objeto Object especificado y devuelve el índice de base cero de la primera aparición en toda la colección ArrayList.

IndexOf(Object, Int32)

Busca el objeto Object especificado y devuelve el índice de base cero de la primera aparición dentro del intervalo de elementos de la matriz ArrayList que abarca desde el índice especificado hasta el último elemento.

IndexOf(Object, Int32, Int32)

Busca el objeto Object especificado y devuelve el índice de base cero de la primera aparición dentro del intervalo de elementos de la matriz ArrayList que comienza en el índice especificado y contiene el número especificado de elementos.

IndexOf(Object)

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

Busca el objeto Object especificado y devuelve el índice de base cero de la primera aparición en toda la colección ArrayList.

public:
 virtual int IndexOf(System::Object ^ value);
public virtual int IndexOf (object value);
public virtual int IndexOf (object? value);
abstract member IndexOf : obj -> int
override this.IndexOf : obj -> int
Public Overridable Function IndexOf (value As Object) As Integer

Parámetros

value
Object

Objeto Object que se va a buscar en la interfaz ArrayList. El valor puede ser null.

Devoluciones

Índice de base cero de la primera aparición de value en la totalidad de ArrayList, si se encuentra; en caso contrario, -1.

Implementaciones

Ejemplos

En el ejemplo de código siguiente se muestra cómo determinar el índice de la primera aparición de un elemento especificado.

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 );
   
   // 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 );
}

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

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

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.
The first occurrence of "the" between index 11 and the end is at index -1.
*/
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)

        ' 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)

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

    End Sub

    Public Shared Sub PrintIndexAndValues(ByVal 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 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.
' The first occurrence of "the" between index 11 and the end is at index -1.
'

Comentarios

ArrayList Se busca hacia delante comenzando en el primer elemento y finalizando en el último elemento.

Este método realiza una búsqueda lineal; por lo tanto, este método es una O(n) operación, donde n es Count.

Este método determina la igualdad llamando a Object.Equals.

A partir de .NET Framework 2.0, este método usa los objetos Equals y CompareTo métodos de la colección en item para determinar si el elemento existe. En las versiones anteriores de .NET Framework, esta determinación se realizó mediante el uso de los Equals métodos y CompareTo del item parámetro en los objetos de la colección.

Consulte también

Se aplica a

IndexOf(Object, Int32)

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

Busca el objeto Object especificado y devuelve el índice de base cero de la primera aparición dentro del intervalo de elementos de la matriz ArrayList que abarca desde el índice especificado hasta el último elemento.

public:
 virtual int IndexOf(System::Object ^ value, int startIndex);
public virtual int IndexOf (object value, int startIndex);
public virtual int IndexOf (object? value, int startIndex);
abstract member IndexOf : obj * int -> int
override this.IndexOf : obj * int -> int
Public Overridable Function IndexOf (value As Object, startIndex As Integer) As Integer

Parámetros

value
Object

Objeto Object que se va a buscar en la interfaz ArrayList. El valor puede ser null.

startIndex
Int32

Índice inicial de base cero de la búsqueda. 0 (cero) es válido en una lista vacía.

Devoluciones

Índice de base cero de la primera aparición de value dentro del intervalo de elementos de ArrayList que abarca desde startIndex hasta el último elemento, si se encuentra; de lo contrario, -1.

Excepciones

startIndex está fuera del intervalo de índices válidos para ArrayList.

Ejemplos

En el ejemplo de código siguiente se muestra cómo determinar el índice de la primera aparición de un elemento especificado.

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 );
   
   // 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 );
}

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

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

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.
The first occurrence of "the" between index 11 and the end is at index -1.
*/
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)

        ' 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)

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

    End Sub

    Public Shared Sub PrintIndexAndValues(ByVal 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 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.
' The first occurrence of "the" between index 11 and the end is at index -1.
'

Comentarios

ArrayList Se busca hacia delante comenzando en startIndex y finalizando en el último elemento.

Este método realiza una búsqueda lineal; por lo tanto, este método es una O(n) operación, donde n es el número de elementos de startIndex hasta el final de ArrayList.

Este método determina la igualdad llamando a Object.Equals.

A partir de .NET Framework 2.0, este método usa los objetos Equals y CompareTo métodos de la colección en item para determinar si el elemento existe. En las versiones anteriores de .NET Framework, esta determinación se realizó mediante el uso de los Equals métodos y CompareTo del item parámetro en los objetos de la colección.

Consulte también

Se aplica a

IndexOf(Object, Int32, Int32)

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

Busca el objeto Object especificado y devuelve el índice de base cero de la primera aparición dentro del intervalo de elementos de la matriz ArrayList que comienza en el índice especificado y contiene el número especificado de elementos.

public:
 virtual int IndexOf(System::Object ^ value, int startIndex, int count);
public virtual int IndexOf (object value, int startIndex, int count);
public virtual int IndexOf (object? value, int startIndex, int count);
abstract member IndexOf : obj * int * int -> int
override this.IndexOf : obj * int * int -> int
Public Overridable Function IndexOf (value As Object, startIndex As Integer, count As Integer) As Integer

Parámetros

value
Object

Objeto Object que se va a buscar en la interfaz ArrayList. El valor puede ser null.

startIndex
Int32

Índice inicial de base cero de la búsqueda. 0 (cero) es válido en una lista vacía.

count
Int32

Número de elementos de la sección en la que se va a realizar la búsqueda.

Devoluciones

Índice de base cero de la primera aparición de value dentro del intervalo de elementos de ArrayList que comienza en startIndex y contiene el número de elementos de count, si se encuentra; de lo contrario, -1.

Excepciones

startIndex está fuera del intervalo de índices válidos para ArrayList.

o bien

count es menor que cero.

o bien

startIndex y count no especifican una sección válida en ArrayList.

Ejemplos

En el ejemplo de código siguiente se muestra cómo determinar el índice de la primera aparición de un elemento especificado.

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 );
   
   // 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 );
}

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

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

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.
The first occurrence of "the" between index 11 and the end is at index -1.
*/
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)

        ' 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)

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

    End Sub

    Public Shared Sub PrintIndexAndValues(ByVal 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 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.
' The first occurrence of "the" between index 11 and the end is at index -1.
'

Comentarios

ArrayList Se busca hacia delante comenzando en startIndex y finalizando en startIndex más count menos 1, si count es mayor que 0.

Este método realiza una búsqueda lineal; por lo tanto, este método es una O(n) operación, donde n es count.

Este método determina la igualdad llamando a Object.Equals.

A partir de .NET Framework 2.0, este método usa los objetos Equals y CompareTo métodos de la colección en item para determinar si el elemento existe. En las versiones anteriores de .NET Framework, esta determinación se realizó mediante el uso de los Equals métodos y CompareTo del item parámetro en los objetos de la colección.

Consulte también

Se aplica a