ArrayList.IndexOf Metoda

Definicja

Zwraca indeks oparty na zerze pierwszego wystąpienia wartości w ArrayList części lub .

Przeciążenia

IndexOf(Object)

Wyszukuje określony Object element i zwraca indeks oparty na zerze pierwszego wystąpienia w całym ArrayListobiekcie .

IndexOf(Object, Int32)

Wyszukuje określony Object i zwraca indeks oparty na zerze pierwszego wystąpienia w zakresie elementów w obiekcie ArrayList , który rozciąga się od określonego indeksu do ostatniego elementu.

IndexOf(Object, Int32, Int32)

Wyszukuje określony Object i zwraca indeks oparty na zerze pierwszego wystąpienia w zakresie elementów w ArrayList indeksie rozpoczynającym się od określonego indeksu i zawiera określoną liczbę elementów.

IndexOf(Object)

Wyszukuje określony Object element i zwraca indeks oparty na zerze pierwszego wystąpienia w całym ArrayListobiekcie .

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

value
Object

Element Object do zlokalizowania w obiekcie ArrayList. Wartość może mieć wartość null.

Zwraca

Int32

Indeks oparty na zerze pierwszego wystąpienia value w całym ArrayListobiekcie , jeśli zostanie znaleziony; w przeciwnym razie wartość -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 przeszukiwany do przodu, zaczynając od pierwszego elementu i kończąc na ostatnim elemecie.

Ta metoda wykonuje wyszukiwanie liniowe; dlatego 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 w obiektach w kolekcji.

Zobacz też

Dotyczy

IndexOf(Object, Int32)

Wyszukuje określony Object i zwraca indeks oparty na zerze pierwszego wystąpienia w zakresie elementów w obiekcie ArrayList , który rozciąga się od określonego indeksu do ostatniego elementu.

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

value
Object

Element Object do zlokalizowania w obiekcie ArrayList. Wartość może mieć wartość null.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania. Wartość 0 (zero) jest prawidłowa na pustej liście.

Zwraca

Int32

Indeks oparty na zerze pierwszego wystąpienia value w zakresie elementów w obiekcie ArrayList , który rozciąga się od startIndex do ostatniego elementu, jeśli zostanie znaleziony; w przeciwnym razie - 1.

Wyjątki

startIndexznajduje się poza zakresem prawidłowych indeksów dla .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, zaczynając od startIndex 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 jest liczbą elementów od startIndex do końca .ArrayList

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 w obiektach w kolekcji.

Zobacz też

Dotyczy

IndexOf(Object, Int32, Int32)

Wyszukuje określony Object i zwraca indeks oparty na zerze pierwszego wystąpienia w zakresie elementów w ArrayList indeksie rozpoczynającym się od określonego indeksu i zawiera określoną liczbę elementów.

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

value
Object

Element Object do zlokalizowania w obiekcie ArrayList. Wartość może mieć wartość null.

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

Int32

Indeks oparty na zerze pierwszego wystąpienia value w zakresie elementów w ArrayList obiekcie rozpoczynającym się od startIndex i zawiera count liczbę elementów, jeśli zostanie znaleziona; w przeciwnym razie wartość -1.

Wyjątki

startIndexznajduje się poza zakresem prawidłowych indeksów dla .ArrayList

-lub- Parametr count ma wartość niższą niż zero.

-lub- startIndex i count nie należy określać 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, zaczynając od startIndex i kończąc na startIndex plus count minus 1, jeśli count jest większy niż 0.

Ta metoda wykonuje wyszukiwanie liniowe; dlatego 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 w obiektach w kolekcji.

Zobacz też

Dotyczy