Array.LastIndexOf メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
オーバーロード
| 名前 | 説明 |
|---|---|
| LastIndexOf(Array, Object) |
指定したオブジェクトを検索し、1 次元 Array全体の最後の出現位置のインデックスを返します。 |
| LastIndexOf(Array, Object, Int32) |
指定したオブジェクトを検索し、最初の要素から指定したインデックスまでの 1 次元 Array 内の要素の範囲内で最後に出現するインデックスを返します。 |
| LastIndexOf(Array, Object, Int32, Int32) |
指定したオブジェクトを検索し、指定した数の要素を含み、指定したインデックスで終わる 1 次元 Array 内の要素の範囲内で最後に出現するインデックスを返します。 |
| LastIndexOf<T>(T[], T) |
指定したオブジェクトを検索し、 Array全体で最後に出現したオブジェクトのインデックスを返します。 |
| LastIndexOf<T>(T[], T, Int32) |
指定したオブジェクトを検索し、最初の要素から指定したインデックスに拡張される Array 内の要素の範囲内で最後に出現するインデックスを返します。 |
| LastIndexOf<T>(T[], T, Int32, Int32) |
指定したオブジェクトを検索し、指定した数の要素を含み、指定したインデックスで終わる Array 内の要素の範囲内で最後に出現するインデックスを返します。 |
LastIndexOf(Array, Object)
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
指定したオブジェクトを検索し、1 次元 Array全体の最後の出現位置のインデックスを返します。
public:
static int LastIndexOf(Array ^ array, System::Object ^ value);
public static int LastIndexOf(Array array, object value);
public static int LastIndexOf(Array array, object? value);
static member LastIndexOf : Array * obj -> int
Public Shared Function LastIndexOf (array As Array, value As Object) As Integer
パラメーター
- value
- Object
arrayで検索するオブジェクト。
返品
見つかった場合は、array全体でvalueが最後に出現するインデックス。それ以外の場合は、配列の下限から 1 を引いた値。
例外
array は nullです。
array は多次元です。
例
次のコード例は、配列内で指定された要素が最後に出現するインデックスを決定する方法を示しています。
let printIndexAndValues (arr: 'a []) =
for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
printfn $"\t[{i}]:\t{arr[i]}"
// Creates and initializes a new Array with three elements of the same value.
let myArray =
[| "the"; "quick"; "brown"; "fox"
"jumps"; "over"; "the"; "lazy"
"dog"; "in"; "the"; "barn" |]
// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray
// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {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.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."
// 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.
// 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 );
void PrintIndexAndValues( Array anArray ) {
for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.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.
*/
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.
注釈
1 次元 Array は、最後の要素から始まり、最初の要素で終わる後方に検索されます。
要素は、 Object.Equals メソッドを使用して、指定した値と比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、その型の Equals 実装が使用されます。
ほとんどの配列の下限は 0 であるため、このメソッドは通常、 value が見つからない場合に -1 を返します。 まれに、配列の下限が Int32.MinValue に等しく、 value が見つからない場合、このメソッドは Int32.MaxValueを返します。これは System.Int32.MinValue - 1。
このメソッドは O(n) 操作です。ここで、nはLengthのarrayです。
このメソッドは、ArrayのEqualsメソッドとCompareToメソッドを使用して、value パラメーターで指定されたObjectが存在するかどうかを判断します。
CompareTo コレクション内のオブジェクトの item パラメーターのメソッド。
こちらもご覧ください
適用対象
LastIndexOf(Array, Object, Int32)
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
指定したオブジェクトを検索し、最初の要素から指定したインデックスまでの 1 次元 Array 内の要素の範囲内で最後に出現するインデックスを返します。
public:
static int LastIndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int LastIndexOf(Array array, object value, int startIndex);
public static int LastIndexOf(Array array, object? value, int startIndex);
static member LastIndexOf : Array * obj * int -> int
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer) As Integer
パラメーター
- value
- Object
arrayで検索するオブジェクト。
- startIndex
- Int32
後方検索の開始インデックス。
返品
最初の要素からstartIndexに拡張されるarray内の要素の範囲内で最後に出現するvalueのインデックス。見つかった場合は、配列の下限から 1 を引いた値。
例外
array は nullです。
startIndex が、 arrayの有効なインデックスの範囲外です。
array は多次元です。
例
次のコード例は、配列内で指定された要素が最後に出現するインデックスを決定する方法を示しています。
let printIndexAndValues (arr: 'a []) =
for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
printfn $"\t[{i}]:\t{arr[i]}"
// Creates and initializes a new Array with three elements of the same value.
let myArray =
[| "the"; "quick"; "brown"; "fox"
"jumps"; "over"; "the"; "lazy"
"dog"; "in"; "the"; "barn" |]
// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray
// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {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.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."
// 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.
// 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 );
void PrintIndexAndValues( Array anArray ) {
for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.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.
*/
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.
注釈
1 次元 Array は、 startIndex から始まり、最初の要素で終わる後方に検索されます。
要素は、 Object.Equals メソッドを使用して、指定した値と比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、その型の Equals 実装が使用されます。
ほとんどの配列の下限は 0 であるため、このメソッドは通常、 value が見つからない場合に -1 を返します。 まれに、配列の下限が Int32.MinValue に等しく、 value が見つからない場合、このメソッドは Int32.MaxValueを返します。これは System.Int32.MinValue - 1。
このメソッドは O(n) 操作です。ここで、 n は、 array の先頭から startIndexまでの要素の数です。
このメソッドは、ArrayのEqualsメソッドとCompareToメソッドを使用して、value パラメーターで指定されたObjectが存在するかどうかを判断します。
こちらもご覧ください
適用対象
LastIndexOf(Array, Object, Int32, Int32)
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
指定したオブジェクトを検索し、指定した数の要素を含み、指定したインデックスで終わる 1 次元 Array 内の要素の範囲内で最後に出現するインデックスを返します。
public:
static int LastIndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int LastIndexOf(Array array, object value, int startIndex, int count);
public static int LastIndexOf(Array array, object? value, int startIndex, int count);
static member LastIndexOf : Array * obj * int * int -> int
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer
パラメーター
- value
- Object
arrayで検索するオブジェクト。
- startIndex
- Int32
後方検索の開始インデックス。
- count
- Int32
検索するセクション内の要素の数。
返品
countで指定され、見つかった場合はstartIndexで終わる要素の数を含む、array内の要素の範囲内で最後に出現するvalueのインデックス。それ以外の場合は、配列の下限から 1 を引きます。
例外
array は nullです。
startIndex が、 arrayの有効なインデックスの範囲外です。
-又は-
count が 0 未満です。
-又は-
startIndex と count は、 arrayで有効なセクションを指定しません。
array は多次元です。
例
次のコード例は、配列内で指定された要素が最後に出現するインデックスを決定する方法を示しています。
LastIndexOfメソッドは後方検索であるため、countは (startIndexから配列の下限と 1 を引いた値) 以下である必要があります。
let printIndexAndValues (arr: 'a []) =
for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
printfn $"\t[{i}]:\t{arr[i]}"
// Creates and initializes a new Array with three elements of the same value.
let myArray =
[| "the"; "quick"; "brown"; "fox"
"jumps"; "over"; "the"; "lazy"
"dog"; "in"; "the"; "barn" |]
// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray
// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {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.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."
// 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.
// 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 );
void PrintIndexAndValues( Array anArray ) {
for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.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.
*/
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.
注釈
1 次元Arrayは、startIndex から後方に検索され、countが 0 より大きい場合は、startIndexマイナス count + 1 で終わる。
要素は、 Object.Equals メソッドを使用して、指定した値と比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、その型のEquals 実装が使用されます。
ほとんどの配列の下限は 0 であるため、このメソッドは通常、 value が見つからない場合に -1 を返します。 まれに、配列の下限が Int32.MinValue に等しく、 value が見つからない場合、このメソッドは Int32.MaxValueを返します。これは System.Int32.MinValue - 1。
このメソッドは O(n) 操作であり、 n は count。
このメソッドは、ArrayのEqualsメソッドとCompareToメソッドを使用して、value パラメーターで指定されたObjectが存在するかどうかを判断します。
こちらもご覧ください
適用対象
LastIndexOf<T>(T[], T)
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
指定したオブジェクトを検索し、 Array全体で最後に出現したオブジェクトのインデックスを返します。
public:
generic <typename T>
static int LastIndexOf(cli::array <T> ^ array, T value);
public static int LastIndexOf<T>(T[] array, T value);
static member LastIndexOf : 'T[] * 'T -> int
Public Shared Function LastIndexOf(Of T) (array As T(), value As T) As Integer
型パラメーター
- T
配列の要素の型。
パラメーター
- array
- T[]
検索する 1 次元の 0 から始まる Array 。
- value
- T
arrayで検索するオブジェクト。
返品
array全体でvalueが最後に出現した場合は 0 から始まるインデックス。それ以外の場合は -1。
例外
array は nullです。
例
次のコード例は、 LastIndexOf メソッドの 3 つのジェネリック オーバーロードをすべて示しています。 インデックス位置 0 とインデックス位置 5 に、1 つのエントリが 2 回表示される文字列の配列が作成されます。 LastIndexOf<T>(T[], T) メソッドのオーバーロードは、末尾から配列全体を検索し、2 番目に出現する文字列を検索します。 LastIndexOf<T>(T[], T, Int32) メソッドのオーバーロードは、インデックス位置 3 で始まり、配列の先頭まで続けて配列を後方に検索し、文字列の最初の出現箇所を検索するために使用されます。 最後に、 LastIndexOf<T>(T[], T, Int32, Int32) メソッドのオーバーロードは、インデックス位置 4 から後方に拡張する 4 つのエントリの範囲を検索するために使用されます (つまり、場所 4、3、2、1 の項目を検索します)。この検索では、その範囲に検索文字列のインスタンスがないため、-1 が返されます。
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
注釈
Arrayは、最後の要素から始まり、最初の要素で終わる後方に検索されます。
要素は、 Object.Equals メソッドを使用して、指定した値と比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、その型の Equals 実装が使用されます。
このメソッドは O(n) 操作です。ここで、nはLengthのarrayです。
こちらもご覧ください
適用対象
LastIndexOf<T>(T[], T, Int32)
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
指定したオブジェクトを検索し、最初の要素から指定したインデックスに拡張される Array 内の要素の範囲内で最後に出現するインデックスを返します。
public:
generic <typename T>
static int LastIndexOf(cli::array <T> ^ array, T value, int startIndex);
public static int LastIndexOf<T>(T[] array, T value, int startIndex);
static member LastIndexOf : 'T[] * 'T * int -> int
Public Shared Function LastIndexOf(Of T) (array As T(), value As T, startIndex As Integer) As Integer
型パラメーター
- T
配列の要素の型。
パラメーター
- array
- T[]
検索する 1 次元の 0 から始まる Array 。
- value
- T
arrayで検索するオブジェクト。
- startIndex
- Int32
後方検索の 0 から始まる開始インデックス。
返品
最初の要素からstartIndexに拡張されるarray内の要素の範囲内で最後に見つかったvalueの 0 から始まるインデックス 。それ以外の場合は -1。
例外
array は nullです。
startIndex が、 arrayの有効なインデックスの範囲外です。
例
次のコード例は、 LastIndexOf メソッドの 3 つのジェネリック オーバーロードをすべて示しています。 インデックス位置 0 とインデックス位置 5 に、1 つのエントリが 2 回表示される文字列の配列が作成されます。 LastIndexOf<T>(T[], T) メソッドのオーバーロードは、末尾から配列全体を検索し、2 番目に出現する文字列を検索します。 LastIndexOf<T>(T[], T, Int32) メソッドのオーバーロードは、インデックス位置 3 で始まり、配列の先頭まで続けて配列を後方に検索し、文字列の最初の出現箇所を検索するために使用されます。 最後に、 LastIndexOf<T>(T[], T, Int32, Int32) メソッドのオーバーロードは、インデックス位置 4 から後方に拡張する 4 つのエントリの範囲を検索するために使用されます (つまり、場所 4、3、2、1 の項目を検索します)。この検索では、その範囲に検索文字列のインスタンスがないため、-1 が返されます。
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
注釈
Arrayは、startIndexから始まり、最初の要素で終わる後方に検索されます。
要素は、 Object.Equals メソッドを使用して、指定した値と比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、その型の Equals 実装が使用されます。
このメソッドは O(n) 操作です。ここで、 n は、 array の先頭から startIndexまでの要素の数です。
こちらもご覧ください
適用対象
LastIndexOf<T>(T[], T, Int32, Int32)
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
- ソース:
- Array.cs
指定したオブジェクトを検索し、指定した数の要素を含み、指定したインデックスで終わる Array 内の要素の範囲内で最後に出現するインデックスを返します。
public:
generic <typename T>
static int LastIndexOf(cli::array <T> ^ array, T value, int startIndex, int count);
public static int LastIndexOf<T>(T[] array, T value, int startIndex, int count);
static member LastIndexOf : 'T[] * 'T * int * int -> int
Public Shared Function LastIndexOf(Of T) (array As T(), value As T, startIndex As Integer, count As Integer) As Integer
型パラメーター
- T
配列の要素の型。
パラメーター
- array
- T[]
検索する 1 次元の 0 から始まる Array 。
- value
- T
arrayで検索するオブジェクト。
- startIndex
- Int32
後方検索の 0 から始まる開始インデックス。
- count
- Int32
検索するセクション内の要素の数。
返品
countで指定された要素数を含み、見つかった場合は startIndex で終わる要素の数を含む、array内の要素の範囲内で最後に出現したvalueの 0 から始まるインデックス。それ以外の場合は -1。
例外
array は nullです。
startIndex が、 arrayの有効なインデックスの範囲外です。
-又は-
count が 0 未満です。
-又は-
startIndex と count は、 arrayで有効なセクションを指定しません。
例
次のコード例は、 LastIndexOf メソッドの 3 つのジェネリック オーバーロードをすべて示しています。 インデックス位置 0 とインデックス位置 5 に、1 つのエントリが 2 回表示される文字列の配列が作成されます。 LastIndexOf<T>(T[], T) メソッドのオーバーロードは、末尾から配列全体を検索し、2 番目に出現する文字列を検索します。 LastIndexOf<T>(T[], T, Int32) メソッドのオーバーロードは、インデックス位置 3 で始まり、配列の先頭まで続けて配列を後方に検索し、文字列の最初の出現箇所を検索するために使用されます。 最後に、 LastIndexOf<T>(T[], T, Int32, Int32) メソッドのオーバーロードは、インデックス位置 4 から後方に拡張する 4 つのエントリの範囲を検索するために使用されます (つまり、場所 4、3、2、1 の項目を検索します)。この検索では、その範囲に検索文字列のインスタンスがないため、-1 が返されます。
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
注釈
countが 0 より大きい場合、Arrayは、startIndex から後方に検索され、startIndex - count + 1 で終わる。
要素は、 Object.Equals メソッドを使用して、指定した値と比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、その型の Equals 実装が使用されます。
このメソッドは O(n) 操作であり、 n は count。