ArrayList.IndexOf 方法

定义

返回 ArrayList 或它的一部分中某个值的第一个匹配项的从零开始的索引。

重载

IndexOf(Object)

搜索指定的 Object,并返回整个 ArrayList 中第一个匹配项的从零开始的索引。

IndexOf(Object, Int32)

搜索指定的 Object,并返回 ArrayList 中从指定索引到最后一个元素的元素范围中第一个匹配项的从零开始索引。

IndexOf(Object, Int32, Int32)

搜索指定的 Object,并返回 ArrayList 中从指定索引开始,并包含指定元素数的元素范围中第一个匹配项的从零开始的索引。

IndexOf(Object)

搜索指定的 Object,并返回整个 ArrayList 中第一个匹配项的从零开始的索引。

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

参数

value
Object

要在 Object 中定位的 ArrayList。 该值可以为 null

返回

Int32

如果找到,则为整个 valueArrayList 第一个匹配项的从零开始的索引;否则为 -1。

实现

示例

下面的代码示例演示如何确定指定元素的第一个匹配项的索引。

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

注解

ArrayList 第一个元素开始,并在最后一个元素处结束,将向前搜索该元素。

此方法执行线性搜索;因此,此方法是一个O(n)操作,其中nCount

此方法通过调用 Object.Equals确定相等性。

从 .NET Framework 2.0 开始,此方法使用集合的对象EqualsCompareTo方法item来确定项是否存在。 在早期版本的.NET Framework中,通过使用集合中对象的参数和CompareTo方法item确定Equals此决定。

另请参阅

适用于

IndexOf(Object, Int32)

搜索指定的 Object,并返回 ArrayList 中从指定索引到最后一个元素的元素范围中第一个匹配项的从零开始索引。

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

参数

value
Object

要在 Object 中定位的 ArrayList。 该值可以为 null

startIndex
Int32

从零开始的搜索的起始索引。 空列表中 0(零)为有效值。

返回

Int32

如果在 ArrayList 中从 startIndex 到最后一个元素的元素范围内找到 value 的第一个匹配项,则为该项的从零开始的索引;否则为 -1。

例外

startIndex 超出了 ArrayList 的有效索引范围。

示例

下面的代码示例演示如何确定指定元素的第一个匹配项的索引。

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

注解

ArrayList 最后一个元素开始 startIndex 和结束,将向前搜索该元素。

此方法执行线性搜索;因此,此方法是一个 O(n) 操作,其中 n 元素数从 startIndex 末尾开始 ArrayList

此方法通过调用 Object.Equals确定相等性。

从 .NET Framework 2.0 开始,此方法使用集合的对象EqualsCompareTo方法item来确定项是否存在。 在早期版本的.NET Framework中,通过使用集合中对象的参数和CompareTo方法item确定Equals此决定。

另请参阅

适用于

IndexOf(Object, Int32, Int32)

搜索指定的 Object,并返回 ArrayList 中从指定索引开始,并包含指定元素数的元素范围中第一个匹配项的从零开始的索引。

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

参数

value
Object

要在 Object 中定位的 ArrayList。 该值可以为 null

startIndex
Int32

从零开始的搜索的起始索引。 空列表中 0(零)为有效值。

count
Int32

要搜索的部分中的元素数。

返回

Int32

如果在 ArrayList 中从 startIndex 开始并包含 count 个元素的元素范围内找到 value 的第一个匹配项,则为该项的从零开始的索引;否则为 -1。

例外

startIndex 超出了 ArrayList 的有效索引范围。

  • 或 - count 小于零。

  • 或 - startIndexcount 未在 ArrayList 中指定有效部分。

示例

下面的代码示例演示如何确定指定元素的第一个匹配项的索引。

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

注解

如果大于 0,countArrayList从加count减 1 开始startIndex``startIndex搜索前方搜索。

此方法执行线性搜索;因此,此方法是一个O(n)操作,其中ncount

此方法通过调用 Object.Equals确定相等性。

从 .NET Framework 2.0 开始,此方法使用集合的对象EqualsCompareTo方法item来确定项是否存在。 在早期版本的.NET Framework中,通过使用集合中对象的参数和CompareTo方法item确定Equals此决定。

另请参阅

适用于