ArrayList.IndexOf Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Zwraca indeks oparty na zerach pierwszego wystąpienia wartości w ArrayList części lub w niej.
Przeciążenia
IndexOf(Object) |
Wyszukuje określony Object element i zwraca indeks oparty na zerowym pierwszym wystąpieniu w całym ArrayListobiekcie . |
IndexOf(Object, Int32) |
Wyszukuje określony Object element i zwraca indeks oparty na zera pierwszego wystąpienia w zakresie elementów w ArrayList obiekcie, który rozciąga się od określonego indeksu do ostatniego elementu. |
IndexOf(Object, Int32, Int32) |
Wyszukuje określony Object i zwraca indeks pierwszego wystąpienia w zakresie elementów w ArrayList określonym indeksie, który rozpoczyna się od określonego indeksu i zawiera określoną liczbę elementów. |
IndexOf(Object)
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- 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
Parametry
Zwraca
Indeks oparty na zerach pierwszego wystąpienia value
w całym ArrayListobiekcie , jeśli zostanie znaleziony; w przeciwnym razie -1.
Implementuje
Przykłady
Poniższy przykład kodu pokazuje, jak określić indeks pierwszego wystąpienia określonego elementu.
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.
'
Uwagi
Element ArrayList jest wyszukiwany do przodu, zaczynając od pierwszego elementu i kończąc na ostatnim elemecie.
Ta metoda wykonuje wyszukiwanie liniowe; w związku z tym ta metoda jest operacją O(n)
, gdzie n
to Count.
Ta metoda określa równość przez wywołanie metody Object.Equals.
Począwszy od .NET Framework 2.0, ta metoda używa metod i CompareTo obiektów Equals kolekcji do item
określenia, czy element istnieje. We wcześniejszych wersjach .NET Framework ta determinacja została wykonana przy użyciu Equals metod item
i CompareTo parametru na obiektach w kolekcji.
Zobacz też
- LastIndexOf(Object)
- Contains(Object)
- Wykonywanie niezależnych od kultury operacji na ciągach w kolekcjach
Dotyczy
IndexOf(Object, Int32)
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- 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
Parametry
- startIndex
- Int32
Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania. Wartość 0 (zero) jest prawidłowa na pustej liście.
Zwraca
Indeks oparty na zerach pierwszego wystąpienia value
w zakresie elementów w ArrayList obiekcie, który rozciąga się od startIndex
do ostatniego elementu, jeśli zostanie znaleziony; w przeciwnym razie -1.
Wyjątki
startIndex
znajduje się poza zakresem prawidłowych indeksów dla elementu ArrayList.
Przykłady
Poniższy przykład kodu pokazuje, jak określić indeks pierwszego wystąpienia określonego elementu.
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.
'
Uwagi
Element ArrayList jest przeszukiwany dalej rozpoczynający się od startIndex
i kończący się na ostatnim elemecie.
Ta metoda wykonuje wyszukiwanie liniowe; w związku z tym ta metoda jest operacją O(n)
, gdzie n
jest liczbą elementów od startIndex
do końca ArrayListelementu .
Ta metoda określa równość przez wywołanie metody Object.Equals.
Począwszy od .NET Framework 2.0, ta metoda używa metod i CompareTo obiektów Equals kolekcji do item
określenia, czy element istnieje. We wcześniejszych wersjach .NET Framework ta determinacja została wykonana przy użyciu Equals metod item
i CompareTo parametru na obiektach w kolekcji.
Zobacz też
- LastIndexOf(Object)
- Contains(Object)
- Wykonywanie niezależnych od kultury operacji na ciągach w kolekcjach
Dotyczy
IndexOf(Object, Int32, Int32)
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- 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
Parametry
- startIndex
- Int32
Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania. Wartość 0 (zero) jest prawidłowa na pustej liście.
- count
- Int32
Liczba elementów w sekcji do wyszukania.
Zwraca
Indeks oparty na zerach pierwszego wystąpienia value
w zakresie elementów, ArrayList które zaczynają się od startIndex
i zawiera count
liczbę elementów, jeśli zostanie znaleziona; w przeciwnym razie -1.
Wyjątki
startIndex
znajduje się poza zakresem prawidłowych indeksów dla elementu ArrayList.
-lub-
Parametr count
ma wartość niższą niż zero.
-lub-
startIndex
i count
nie określają prawidłowej sekcji w pliku ArrayList.
Przykłady
Poniższy przykład kodu pokazuje, jak określić indeks pierwszego wystąpienia określonego elementu.
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.
'
Uwagi
Element ArrayList jest wyszukiwany do przodu rozpoczynający się od i kończący startIndex
się na startIndex
plus count
minus 1, jeśli count
jest większy niż 0.
Ta metoda wykonuje wyszukiwanie liniowe; w związku z tym ta metoda jest operacją O(n)
, gdzie n
to count
.
Ta metoda określa równość przez wywołanie metody Object.Equals.
Począwszy od .NET Framework 2.0, ta metoda używa metod i CompareTo obiektów Equals kolekcji do item
określenia, czy element istnieje. We wcześniejszych wersjach .NET Framework ta determinacja została wykonana przy użyciu Equals metod item
i CompareTo parametru na obiektach w kolekcji.
Zobacz też
- LastIndexOf(Object)
- Contains(Object)
- Wykonywanie niezależnych od kultury operacji na ciągach w kolekcjach