ArrayList.IndexOf Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Devuelve el índice de base cero de la primera aparición de un valor en el ArrayList o en una parte de él.
Sobrecargas
IndexOf(Object) |
Busca el Object especificado y devuelve el índice de base cero de la primera aparición en toda la ArrayList. |
IndexOf(Object, Int32) |
Busca el Object especificado y devuelve el índice de base cero de la primera aparición dentro del intervalo de elementos del ArrayList que se extiende desde el índice especificado hasta el último elemento. |
IndexOf(Object, Int32, Int32) |
Busca el Object especificado y devuelve el índice de base cero de la primera aparición dentro del intervalo de elementos de la 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
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
Devoluciones
Índice de base cero de la primera aparición de value
dentro de toda la ArrayList, si se encuentra; de lo 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
El ArrayList se busca hacia delante a partir del primer elemento y termina en el último elemento.
Este método realiza una búsqueda lineal; por lo tanto, este método es una operación de O(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 métodos Equals y CompareTo de los objetos de la colección en item
para determinar si existe un elemento. En las versiones anteriores de .NET Framework, esta determinación se realizó mediante los métodos Equals y CompareTo del parámetro item
en los objetos de la colección.
Consulte también
- LastIndexOf(Object)
- Contains(Object)
- realizar operaciones de cadena de Culture-Insensitive en colecciones
Se aplica a
IndexOf(Object, Int32)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
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
- 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 del ArrayList que se extiende de startIndex
al último elemento, si se encuentra; de lo contrario, -1.
Excepciones
startIndex
está fuera del intervalo de índices válidos para el 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
El ArrayList se busca hacia delante a partir de startIndex
y finaliza en el último elemento.
Este método realiza una búsqueda lineal; por lo tanto, este método es una operación de O(n)
, donde n
es el número de elementos de startIndex
al final del ArrayList.
Este método determina la igualdad llamando a Object.Equals.
A partir de .NET Framework 2.0, este método usa los métodos Equals y CompareTo de los objetos de la colección en item
para determinar si existe un elemento. En las versiones anteriores de .NET Framework, esta determinación se realizó mediante los métodos Equals y CompareTo del parámetro item
en los objetos de la colección.
Consulte también
- LastIndexOf(Object)
- Contains(Object)
- realizar operaciones de cadena de Culture-Insensitive en colecciones
Se aplica a
IndexOf(Object, Int32, Int32)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
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
- 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 que se va a buscar.
Devoluciones
Índice de base cero de la primera aparición de value
dentro del intervalo de elementos del ArrayList que comienza en startIndex
y contiene count
número de elementos, si se encuentra; de lo contrario, -1.
Excepciones
startIndex
está fuera del intervalo de índices válidos para el ArrayList.
-o-
count
es menor que cero.
-o-
startIndex
y count
no especifican una sección válida en el 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
El ArrayList se busca a partir de startIndex
y termina 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 operación de O(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 métodos Equals y CompareTo de los objetos de la colección en item
para determinar si existe un elemento. En las versiones anteriores de .NET Framework, esta determinación se realizó mediante los métodos Equals y CompareTo del parámetro item
en los objetos de la colección.
Consulte también
- LastIndexOf(Object)
- Contains(Object)
- realizar operaciones de cadena de Culture-Insensitive en colecciones