C#でリストの指定されたインデックスから要素を削除するにはどうすればよいですか?
リストの指定されたインデックスから要素を削除するには、コードは次のとおりです-
例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args){
List<string> list = new List<string>();
list.Add("Ryan");
list.Add("Kevin");
list.Add("Andre");
list.Add("Tom");
list.Add("Fred");
list.Add("Jason");
list.Add("Jacob");
list.Add("David");
Console.WriteLine("Count of elements in the List = "+list.Count);
Console.WriteLine("Enumerator iterates through the list elements...");
List<string>.Enumerator demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
list.RemoveAt(5);
Console.WriteLine("\nCount of elements in the List [UPDATED] = "+list.Count);
Console.WriteLine("Enumerator iterates through the list elements...[UPDATED]");
demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
}
} 出力
これにより、次の出力が生成されます-
Count of elements in the List = 8 Enumerator iterates through the list elements... Ryan Kevin Andre Tom Fred Jason Jacob David Count of elements in the List [UPDATED] = 7 Enumerator iterates through the list elements...[UPDATED] Ryan Kevin Andre Tom Fred Jacob David
例
別の例を見てみましょう-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args){
List<int> list = new List<int>();
list.Add(25);
list.Add(50);
list.Add(75);
list.Add(100);
list.Add(200);
Console.WriteLine("Count of elements in the List = "+list.Count);
list.RemoveAt(2);
Console.WriteLine("\nCount of elements in the List [UPDATED] = "+list.Count);
}
} 出力
これにより、次の出力が生成されます-
Count of elements in the List = 5 Count of elements in the List [UPDATED] = 4
-
Pythonでリストの最後から2番目の要素を取得するにはどうすればよいですか?
リストオブジェクトを含むPythonシーケンスにより、インデックスを作成できます。リスト内の任意の要素には、ゼロベースのインデックスを使用してアクセスできます。インデックスが負の数の場合、インデックスのカウントは最後から始まります。リストの最後から2番目の要素が必要なので、インデックスとして-2を使用します。 >>> L1=[1,2,3,4,5] >>> print (L1[-2]) 4
-
Pythonでリストの最後の要素を取得するにはどうすればよいですか?
リストオブジェクトを含むPythonシーケンスにより、インデックスを作成できます。リスト内の任意の要素には、ゼロベースのインデックスを使用してアクセスできます。インデックスが負の数の場合、インデックスのカウントは最後から始まります。リストの最後の要素が必要なので、インデックスとして-1を使用します。 >>> L1=[1,2,3,4,5] >>> print (L1[-1]) 5