ArrayList.LastIndexOf メソッド (Object)
指定した Object を検索し、 ArrayList 全体内で最後に見つかった位置の 0 から始まるインデックスを返します。
Overloads Public Overridable Function LastIndexOf( _
ByVal value As Object _) As Integer
[C#]
public virtual int LastIndexOf(objectvalue);
[C++]
public: virtual int LastIndexOf(Object* value);
[JScript]
public function LastIndexOf(
value : Object) : int;
パラメータ
戻り値
ArrayList 全体内で value が見つかった場合は、最後に見つかった位置の 0 から始まるインデックス番号。それ以外の場合は -1。
解説
ArrayList 内で、検索は最後の要素から開始して逆方向に進み、最初の要素で終了します。
このメソッドは順次検索を実行します。したがって、平均検索時間は Count に比例します。つまり、このメソッドは O(n) 操作です。ここで、 n は Count です。
使用例
最後に見つかった指定要素のインデックスを確認する方法を次の例に示します。
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)
' Searches for the last occurrence of the duplicated value.
Dim myString As [String] = "the"
Dim myIndex As Integer = myAL.LastIndexOf(myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL.LastIndexOf(myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf(myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 10 and index 6 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 last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 10 and index 6 is at index 10.
'
[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 );
// Searches for the last occurrence of the duplicated value.
String myString = "the";
int myIndex = myAL.LastIndexOf( myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL.LastIndexOf( myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf( myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 6 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 10 and index 6 is at index 10.
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable* myList );
int 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 last occurrence of the duplicated value.
String* myString = "the";
int myIndex = myAL->LastIndexOf( myString );
Console::WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, __box(myIndex) );
// Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL->LastIndexOf( myString, 8 );
Console::WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, __box(myIndex) );
// Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL->LastIndexOf( myString, 10, 6 );
Console::WriteLine( "The last occurrence of \"{0}\" between index 10 and index 6 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 10 and index 6 is at index 10.
*/
[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 );
// Searches for the last occurrence of the duplicated value.
var myString : String = "the";
var myIndex : int = myAL.LastIndexOf( myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL.LastIndexOf( myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf( myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 6 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 10 and index 6 is at index 10.
*/
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard
参照
ArrayList クラス | ArrayList メンバ | System.Collections 名前空間 | ArrayList.LastIndexOf オーバーロードの一覧 | IndexOf | Contains | カルチャを認識しないコレクションの操作の実行