指定したオブジェクトを検索し、指定したインデックスで終了する、指定した要素数を含む 1 次元 Array の範囲内でそのオブジェクトが最後に見つかった位置のインデックス番号を返します。
Overloads Public Shared Function LastIndexOf( _
ByVal array As Array, _ ByVal value As Object, _ ByVal startIndex As Integer, _ ByVal count As Integer _) As Integer
[C#]
public static int LastIndexOf(Arrayarray,objectvalue,intstartIndex,intcount);
[C++]
public: static int LastIndexOf(Array* array,Object* value,intstartIndex,intcount);
[JScript]
public static function LastIndexOf(
array : Array,value : Object,startIndex : int,count : int) : int;
パラメータ
- array
検索する 1 次元 Array 。 - value
array 内で検索するオブジェクト。 - startIndex
逆方向検索の開始インデックス。 - count
検索対象の範囲内にある要素の数。
戻り値
startIndex で終了する、count で指定した要素数を含む array の範囲内で value が見つかった場合は、その値が最後に見つかった位置のインデックス番号。それ以外の場合は、配列の下限 - 1。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | array が null 参照 (Visual Basic では Nothing) です。 |
ArgumentOutOfRangeException | startIndex が array の有効なインデックスの範囲外の値です。
または count が 0 未満です。 または startIndex および count が array 内の有効な範囲を指定していません。 |
RankException | array が多次元です。 |
解説
1 次元 Array 内で、検索は startIndex から開始して逆方向に進み、 startIndex- count + 1 の位置で終了します。
要素は、 Object.Equals メソッドを使用して、指定した値と比較されます。要素の型が非組み込み型 (ユーザー定義型) の場合は、この型の Equals 実装が使用されます。
ほとんどの配列の下限は 0 であるため、 value が見つからない場合、このメソッドは通常 -1 を返します。配列の下限が Int32.MinValue に等しく、 value が見つからないまれな場合、このメソッドは Int32.MaxValue を返します。これは System.Int32.MinValue - 1
です。
使用例
配列内で最後に見つかった指定要素のインデックスを確認する方法を次のコード例で示します。 LastIndexOf メソッドは逆方向検索であるため、count が (startIndex- 配列の下限 + 1) 以下である必要があります。
Imports System
Imports Microsoft.VisualBasic
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array with three elements of
' the same value.
Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
myArray.SetValue("the", 0)
myArray.SetValue("quick", 1)
myArray.SetValue("brown", 2)
myArray.SetValue("fox", 3)
myArray.SetValue("jumps", 4)
myArray.SetValue("over", 5)
myArray.SetValue("the", 6)
myArray.SetValue("lazy", 7)
myArray.SetValue("dog", 8)
myArray.SetValue("in", 9)
myArray.SetValue("the", 10)
myArray.SetValue("barn", 11)
' Displays the values of the Array.
Console.WriteLine("The Array contains the following values:")
PrintIndexAndValues(myArray)
' Searches for the last occurrence of the duplicated value.
Dim myString As String = "the"
Dim myIndex As Integer = Array.LastIndexOf(myArray, 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 Array.
myIndex = Array.LastIndexOf(myArray, 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 Array. Note that the start index is greater than the end
' index because the search is done backward.
myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
+ "and index 10 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myArray As Array)
Dim i As Integer
For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myArray.GetValue(i))
Next i
End Sub
End Class
' This code produces the following output.
'
' The Array 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 5 and index 10 is at index 10.
[C#]
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(String), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
String myString = "the";
int myIndex = Array.LastIndexOf( myArray, 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 Array.
myIndex = Array.LastIndexOf( myArray, 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 Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}
public static void PrintIndexAndValues( Array myArray ) {
for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
}
}
/*
This code produces the following output.
The Array 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 5 and index 10 is at index 10.
*/
[C++]
#using <mscorlib.dll>
using namespace System;
void PrintIndexAndValues( Array* myArray );
void main() {
// Creates and initializes a new Array instance with three elements of the same value.
Array* myArray = Array::CreateInstance( __typeof(String), 12 );
myArray->SetValue( S"the", 0 );
myArray->SetValue( S"quick", 1 );
myArray->SetValue( S"brown", 2 );
myArray->SetValue( S"fox", 3 );
myArray->SetValue( S"jumped", 4 );
myArray->SetValue( S"over", 5 );
myArray->SetValue( S"the", 6 );
myArray->SetValue( S"lazy", 7 );
myArray->SetValue( S"dog", 8 );
myArray->SetValue( S"in", 9 );
myArray->SetValue( S"the", 10 );
myArray->SetValue( S"barn", 11 );
// Displays the values of the Array.
Console::WriteLine( "The Array instance contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
String* myString = "the";
int myIndex = Array::LastIndexOf( myArray, 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 Array.
myIndex = Array::LastIndexOf( myArray, 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 Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
Console::WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, __box(myIndex));
}
void PrintIndexAndValues( Array* myArray ) {
for ( int i = myArray->GetLowerBound(0); i <= myArray->GetUpperBound(0); i++ )
Console::WriteLine( "\t[{0}]:\t{1}", __box(i), myArray->GetValue( i ) );
}
/*
This code produces the following output.
The Array 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 5 and index 10 is at index 10.
*/
[JScript]
import System;
// Creates and initializes a new Array with three elements of the same value.
var myArray : System.Array= System.Array.CreateInstance( System.String, 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumped", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
var myString : String = "the";
var myIndex : int = System.Array.LastIndexOf( myArray, 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 Array.
myIndex = System.Array.LastIndexOf( myArray, 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 Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = System.Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
function PrintIndexAndValues( myArray : System.Array ) {
for ( var i : int = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
}
/*
This code produces the following output.
The Array 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 5 and index 10 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 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
Array クラス | Array メンバ | System 名前空間 | Array.LastIndexOf オーバーロードの一覧 | IndexOf | カルチャを認識しない配列の操作の実行