C#で配列の最後の要素のインデックスを見つける
配列の最後の要素のインデックスを見つけるためのコードは次のとおりです-
例
using System;
public class Demo {
public static void Main(){
string[] products = new string[] { "Andy", "Mark", "Gary", "Andre"};
Console.WriteLine("One or more name begin with 'A'? = {0}",
Array.Exists(products, ele => ele.StartsWith("A")));
Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
Console.WriteLine("Is the array read only? = " + products.IsReadOnly);
Console.WriteLine("Is the array synchronized? = " + products.IsSynchronized);
Console.WriteLine("Index of the 1st element = "+products.GetLowerBound(0));
Console.WriteLine("Index of the last element = "+products.GetUpperBound(0));
}
} 出力
これにより、次の出力が生成されます-
One or more name begin with 'A'? = True Is the array having fixed size? = True Is the array read only? = False Is the array synchronized? = False Index of the 1st element = 0 Index of the last element = 3
例
別の例を見てみましょう-
using System;
public class Demo {
public static void Main(){
string[] products = new string[] { };
Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
Console.WriteLine("Is the array read only? = " + products.IsReadOnly);
Console.WriteLine("Is the array synchronized? = " + products.IsSynchronized);
Console.WriteLine("Index of the 1st element = "+products.GetLowerBound(0));
Console.WriteLine("Index of the last element = "+products.GetUpperBound(0));
}
} 出力
これにより、次の出力が生成されます-
Is the array having fixed size? = True Is the array read only? = False Is the array synchronized? = False Index of the 1st element = 0 Index of the last element = -1
-
配列から最後の要素を取得するC#プログラム
まず、配列を設定します- string[] str = new string[]{ "Java", "HTML", "jQuery", "JavaScript", "Bootstrap" }; 最後の要素の値を取得するには、長さを取得し、次の値を表示します- str[str.Length - 1] 上記は最後の要素を返します。 これが完全なコードです- 例 us
-
Pythonでリストの最後の要素を取得するにはどうすればよいですか?
リストオブジェクトを含むPythonシーケンスにより、インデックスを作成できます。リスト内の任意の要素には、ゼロベースのインデックスを使用してアクセスできます。インデックスが負の数の場合、インデックスのカウントは最後から始まります。リストの最後の要素が必要なので、インデックスとして-1を使用します。 >>> L1=[1,2,3,4,5] >>> print (L1[-1]) 5