ArrayList.IndexOf メソッド
ArrayList 内またはその一部にある値のうち、最初に出現する値の、0 から始まるインデックス番号を返します。
オーバーロードの一覧
指定した Object を検索し、 ArrayList 全体内で最初に見つかった位置の 0 から始まるインデックスを返します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function IndexOf(Object) As Integer Implements IList.IndexOf
[JScript] public function IndexOf(Object) : int;
指定した Object を検索し、指定したインデックスから最後の要素までの ArrayList のセクション内で最初に出現する位置の 0 から始まるインデックス番号を返します。
[Visual Basic] Overloads Public Overridable Function IndexOf(Object, Integer) As Integer
指定した Object を検索し、指定したインデックスから始まって指定した数の要素を格納する ArrayList のセクション内で最初に出現する位置の 0 から始まるインデックス番号を返します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function IndexOf(Object, Integer, Integer) As Integer
使用例
最初に見つかった指定要素のインデックスを確認する方法を次の例に示します。
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
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)
End Sub 'Main
Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
Dim i As Integer = 0
Dim myEnumerator As System.Collections.IEnumerator = myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab + "{1}", i, myEnumerator.Current)
i = i + 1
End While
Console.WriteLine()
End Sub 'PrintIndexAndValues
End Class 'SamplesArrayList
' 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.
'
[C#]
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 );
}
public static void PrintIndexAndValues( IEnumerable myList ) {
int i = 0;
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
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.
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable* myList );
void main() {
// Creates and initializes a new ArrayList instance with three elements of the same value.
ArrayList* myAL = new ArrayList();
myAL->Add( S"the" );
myAL->Add( S"quick" );
myAL->Add( S"brown" );
myAL->Add( S"fox" );
myAL->Add( S"jumped" );
myAL->Add( S"over" );
myAL->Add( S"the" );
myAL->Add( S"lazy" );
myAL->Add( S"dog" );
myAL->Add( S"in" );
myAL->Add( S"the" );
myAL->Add( S"barn" );
// Displays the values of the ArrayList.
Console::WriteLine( "The ArrayList instance contains the following values:" );
PrintIndexAndValues( myAL );
// Searches 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, __box(myIndex) );
// Searches 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, __box(myIndex) );
// Searches 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, __box(myIndex) );
}
void PrintIndexAndValues( IEnumerable* myList ) {
int i = 0;
System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
while ( myEnumerator->MoveNext() )
Console::WriteLine( "\t[{0}]:\t{1}", __box(i++), myEnumerator->Current );
Console::WriteLine();
}
/*
This code produces the following output.
The ArrayList instance contains the following values:
[0]: the
[1]: quick
[2]: brown
[3]: fox
[4]: jumped
[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.
*/
[JScript]
import System;
import System.Collections;
// Creates and initializes a new ArrayList with three elements of the same value.
var myAL : ArrayList = new ArrayList();
myAL.Add( "the" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
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.
var myString : String = "the";
var myIndex : int = 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 );
function PrintIndexAndValues( myList : IEnumerable) {
var i : int = 0;
var myEnumerator : System.Collections.IEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
Console.WriteLine();
}
/*
This code produces the following output.
The ArrayList contains the following values:
[0]: the
[1]: quick
[2]: brown
[3]: fox
[4]: jumped
[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.
*/