ArrayList.IndexOf メソッド

定義

ArrayList 全体またはその一部において、最初に値が出現した位置のインデックス番号 (0 から始まる) を返します。

オーバーロード

IndexOf(Object)

指定した Object を検索し、ArrayList 全体内で最初に見つかった位置の 0 から始まるインデックスを返します。

IndexOf(Object, Int32)

指定した Object を検索し、指定したインデックスから最後の要素までの ArrayList 内の要素の範囲内で最初に出現する位置の 0 から始まるインデックス番号を返します。

IndexOf(Object, Int32, Int32)

指定した Object を検索し、指定したインデックスから始まって指定した数の要素を格納する ArrayList 内の要素の範囲内で最初に出現する位置の 0 から始まるインデックス番号を返します。

IndexOf(Object)

指定した Object を検索し、ArrayList 全体内で最初に見つかった位置の 0 から始まるインデックスを返します。

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

ArrayList 内で検索される Object。 値として null を指定できます。

戻り値

Int32

ArrayList 全体を対象に value を検索し、見つかった場合は、インデックス番号の最も小さい要素の 0 から始まるインデックス番号、それ以外の場合は -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では、この決定は、コレクション内のitemオブジェクトのパラメーターとCompareToメソッドを使用してEquals行われました。

こちらもご覧ください

適用対象

IndexOf(Object, Int32)

指定した Object を検索し、指定したインデックスから最後の要素までの ArrayList 内の要素の範囲内で最初に出現する位置の 0 から始まるインデックス番号を返します。

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

ArrayList 内で検索される Object。 値として null を指定できます。

startIndex
Int32

検索の開始位置を示す 0 から始まるインデックス。 空のリストでは 0 (ゼロ) は有効です。

戻り値

Int32

startIndex から最後の要素までの ArrayList 内の要素の範囲内で value が見つかった場合は、最初に見つかった位置の 0 から始まるインデックス番号。それ以外の場合は -1。

例外

startIndexArrayListの有効なインデックスの範囲外です。

次のコード例は、指定した要素が最初に出現するインデックスを決定する方法を示しています。

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、要素startIndexArrayListの数は.

このメソッドは、呼び出 Object.Equalsすことによって等価性を決定します。

.NET Framework 2.0 以降、このメソッドはコレクションのオブジェクトEqualsCompareToメソッドをitem使用して、項目が存在するかどうかを判断します。 以前のバージョンの.NET Frameworkでは、この決定は、コレクション内のitemオブジェクトのパラメーターとCompareToメソッドを使用してEquals行われました。

こちらもご覧ください

適用対象

IndexOf(Object, Int32, Int32)

指定した Object を検索し、指定したインデックスから始まって指定した数の要素を格納する ArrayList 内の要素の範囲内で最初に出現する位置の 0 から始まるインデックス番号を返します。

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

ArrayList 内で検索される Object。 値として null を指定できます。

startIndex
Int32

検索の開始位置を示す 0 から始まるインデックス。 空のリストでは 0 (ゼロ) は有効です。

count
Int32

検索対象の範囲内にある要素の数。

戻り値

Int32

startIndex から始まって count 個の要素を格納する ArrayList 内の要素の範囲内で value が見つかった場合は、最初に見つかった位置の 0 から始まるインデックス番号。それ以外の場合は -1。

例外

startIndexArrayListの有効なインデックスの範囲外です。

  • または - count が 0 未満です。

  • または - startIndex および countArrayList 内の正しいセクションを指定していません。

次のコード例は、指定した要素が最初に出現するインデックスを決定する方法を示しています。

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 0 より大きい場合countは、プラスcountマイナス 1 でstartIndex``startIndex始まり、終わる順に検索されます。

このメソッドは線形検索を実行します。したがって、このメソッドはO(n)操作であり、ここでn``count.

このメソッドは、呼び出 Object.Equalsすことによって等価性を決定します。

.NET Framework 2.0 以降、このメソッドはコレクションのオブジェクトEqualsCompareToメソッドをitem使用して、項目が存在するかどうかを判断します。 以前のバージョンの.NET Frameworkでは、この決定は、コレクション内のitemオブジェクトのパラメーターとCompareToメソッドを使用してEquals行われました。

こちらもご覧ください

適用対象