C#のArrayListで指定されたインデックスの要素を取得または設定します
ArrayListの指定されたインデックスで要素を取得または設定するには、コードは次のとおりです-
例
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("Laptop");
arrList.Add("Desktop");
arrList.Add("Notebook");
arrList.Add("Ultrabook");
arrList.Add("Tablet");
arrList.Add("Headphone");
arrList.Add("Speaker");
Console.WriteLine("Elements in ArrayList...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("Element at index 5 = " + arrList[5]);
}
} 出力
これにより、次の出力が生成されます-
Elements in ArrayList... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 5 = Headphone
例
別の例を見てみましょう-
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("Laptop");
arrList.Add("Desktop");
arrList.Add("Notebook");
arrList.Add("Ultrabook");
arrList.Add("Tablet");
arrList.Add("Headphone");
arrList.Add("Speaker");
Console.WriteLine("Elements in ArrayList...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("Element at index 5 = " + arrList[5]);
arrList[5] = "SSD";
Console.WriteLine("Element at index 5 (Updated) = " + arrList[5]);
}
} 出力
これにより、次の出力が生成されます-
Elements in ArrayList... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 5 = Headphone Element at index 5 (Updated) = SSD
-
リスト内の要素のインデックスを検索するC#プログラム
リストを設定して要素を追加する- List<int> val = new List<int>(); // integer elements val.Add(35); val.Add(55); val.Add(68); ここで、要素68のインデックスを見つける必要があるとします。そのためには、IndexOf()メソッド-を使用します。 int index = val.IndexOf(68); これが完全なコードです- 例 using System; using System.Collections.Generic; public class Demo {  
-
C#のArrayListクラスのCountプロパティとは何ですか?
ArrayListクラスのCountプロパティは、ArrayListの要素の数をカウントします。 まず、ArrayListに要素を追加します- ArrayList arrList = new ArrayList(); arrList.Add(98); arrList.Add(55); arrList.Add(65); arrList.Add(34); 次に、配列リストの数を取得します- arrList.Count 以下は、C#でCountプロパティを実装するためのコードです- 例 using System; using System.Collections; class Demo {