C#のArray.LastIndexOf()メソッド
C#のArray.LastIndexOf()メソッドは、指定されたオブジェクトを検索するために使用され、1次元配列全体の中で最後に出現したインデックスを返します。
構文
public static int LastIndexOf (Array arr, object val);
上記では、arrは検索する1次元配列であり、valはarrで検索するオブジェクトです。
例
using System;
public class Demo {
public static void Main() {
string[] strArr = {"John", "Tim", "Fedric", "Gary", "Harry", "Damien", "David", "Harry"};
Array.Sort(strArr);
Console.WriteLine("Array elements...");
foreach(string s in strArr) {
Console.WriteLine(s);
}
Console.Write("Element Gary is at index = " + Array.BinarySearch(strArr, "Gary"));
Console.Write("\nElement Tom is at index = " + Array.BinarySearch(strArr, "Tom"));
Console.Write("\nLast index of element Harry = " + Array.LastIndexOf(strArr, "Harry"));
}
} 出力
Array elements... Damien David Fedric Gary Harry Harry John Tim Element Gary is at index = 3 Element Tom is at index = -9 Last index of element Harry = 5
例
using System;
public class Demo {
public static void Main() {
int[] intArr = {5, 10, 15, 20, 15, 25, 30};
Array.Sort(intArr);
Console.WriteLine("Array elements...");
foreach(int i in intArr) {
Console.WriteLine(i);
}
Console.Write("Element 25 is at index = " + Array.BinarySearch(intArr, 20));
Console.Write("\nLast index of element 15 = " + Array.LastIndexOf(intArr, 15));
Console.Write("\nLast index of element 50 = " + Array.LastIndexOf(intArr, 50));
}
} 出力
Array elements... 5 10 15 15 20 25 30 Element 25 is at index = 4 Last index of element 15 = 3 Last index of element 50 = -1
-
JavaScript Array.from()メソッド
Array.from()は、指定された配列インスタンスから新しい配列オブジェクトを作成します。 以下は、配列from()関数のコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Docume
-
JavaScriptのarray.entries()メソッド。
JavaScriptのarray.entries()メソッドは、キーと値のペアを配列イテレータオブジェクトとして返します。 以下は、array.entries()メソッドのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /&