ArrayList.IndexOf Método
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Retorna o índice baseado em zero da primeira ocorrência de um valor no ArrayList ou em uma parte dele.
Sobrecargas
IndexOf(Object) |
Pesquisa o Object especificado e retorna o índice de base zero da primeira ocorrência dentro de todo o ArrayList. |
IndexOf(Object, Int32) |
Pesquisa o Object especificado e retorna o índice baseado em zero da primeira ocorrência dentro do intervalo de elementos no ArrayList que se estende do índice especificado ao último elemento. |
IndexOf(Object, Int32, Int32) |
Pesquisa o Object especificado e retorna o índice baseado em zero da primeira ocorrência dentro de um intervalo de elementos no ArrayList que começa no índice especificado e contém o número de elementos especificado. |
IndexOf(Object)
- Origem:
- ArrayList.cs
- Origem:
- ArrayList.cs
- Origem:
- 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
Retornos
O índice baseado em zero da primeira ocorrência de value
em todo o ArrayList, se encontrado; caso contrário, -1.
Implementações
Exemplos
O exemplo de código a seguir mostra como determinar o índice da primeira ocorrência de um 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.
'
Comentários
O ArrayList é pesquisado para frente começando no primeiro elemento e terminando no último elemento.
Esse método executa uma pesquisa linear; portanto, esse método é uma O(n)
operação, em que n
é Count.
Este método determina a igualdade chamando Object.Equals.
A partir do .NET Framework 2.0, esse método usa os objetos e CompareTo métodos Equals da coleção em item
para determinar se o item existe. Em versões anteriores do .NET Framework, essa decisão era tomada usando-se os métodos Equals e CompareTo do parâmetro item
nos objetos na coleção.
Confira também
- LastIndexOf(Object)
- Contains(Object)
- Executando operações de cadeia de caracteres Culture-Insensitive em coleções
Aplica-se a
IndexOf(Object, Int32)
- Origem:
- ArrayList.cs
- Origem:
- ArrayList.cs
- Origem:
- 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
O índice inicial com base em zero da pesquisa. 0 (zero) é válido em uma lista vazia.
Retornos
O índice baseado em zero da primeira ocorrência de value
dentro do intervalo de elementos no ArrayList que se estende do startIndex
ao último elemento, se for encontrado; caso contrário, -1.
Exceções
startIndex
está fora do intervalo de índices válidos para o ArrayList.
Exemplos
O exemplo de código a seguir mostra como determinar o índice da primeira ocorrência de um 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.
'
Comentários
O ArrayList é pesquisado para frente começando em startIndex
e terminando no último elemento.
Esse método executa uma pesquisa linear; portanto, esse método é uma O(n)
operação, em que n
é o número de elementos de startIndex
até o final do ArrayList.
Este método determina a igualdade chamando Object.Equals.
A partir do .NET Framework 2.0, esse método usa os objetos e CompareTo métodos Equals da coleção em item
para determinar se o item existe. Em versões anteriores do .NET Framework, essa decisão era tomada usando-se os métodos Equals e CompareTo do parâmetro item
nos objetos na coleção.
Confira também
- LastIndexOf(Object)
- Contains(Object)
- Executando operações de cadeia de caracteres Culture-Insensitive em coleções
Aplica-se a
IndexOf(Object, Int32, Int32)
- Origem:
- ArrayList.cs
- Origem:
- ArrayList.cs
- Origem:
- 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
O índice inicial com base em zero da pesquisa. 0 (zero) é válido em uma lista vazia.
- count
- Int32
O número de elementos na seção a ser pesquisada.
Retornos
O índice baseado em zero da primeira ocorrência de value
dentro do intervalo de elementos no ArrayList que começa em startIndex
e contém o número de elementos count
, se encontrado; caso contrário, -1.
Exceções
startIndex
está fora do intervalo de índices válidos para o ArrayList.
- ou -
count
é menor que zero.
- ou -
startIndex
e count
não especificam uma seção válida no ArrayList.
Exemplos
O exemplo de código a seguir mostra como determinar o índice da primeira ocorrência de um 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.
'
Comentários
O ArrayList é pesquisado para frente começando em startIndex
e terminando em startIndex
mais count
menos 1, se count
for maior que 0.
Esse método executa uma pesquisa linear; portanto, esse método é uma O(n)
operação, em que n
é count
.
Este método determina a igualdade chamando Object.Equals.
A partir do .NET Framework 2.0, esse método usa os objetos e CompareTo métodos Equals da coleção em item
para determinar se o item existe. Em versões anteriores do .NET Framework, essa decisão era tomada usando-se os métodos Equals e CompareTo do parâmetro item
nos objetos na coleção.
Confira também
- LastIndexOf(Object)
- Contains(Object)
- Executando operações de cadeia de caracteres Culture-Insensitive em coleções