C#
 Computer >> コンピューター >  >> プログラミング >> C#

【C#】Listの指定インデックスにある要素を取得・設定する方法

C#の List<T> では、インデクサー(list[インデックス])を使うことで、指定した位置にある要素を簡単に取得したり設定(更新)したりできます。
インデックスは0から始まるため、最初の要素にアクセスする場合は list[0] を指定します。

例1:指定インデックスの要素を取得する

以下のコードでは、文字列型のリストを作成し、各インデックスの要素をコンソールに出力しています。

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(String[] args) {
      List<String> list = new List<String>();
      list.Add("100");
      list.Add("200");
      list.Add("300");
      list.Add("400");
      list.Add("500");
      list.Add("600");
      list.Add("700");
      list.Add("800");
      list.Add("900");
      list.Add("1000");
      Console.WriteLine("Element at index 0 = " + list[0]);
      Console.WriteLine("Element at index 1 = " + list[1]);
      Console.WriteLine("Element at index 2 = " + list[2]);
      Console.WriteLine("Element at index 3 = " + list[3]);
      Console.WriteLine("Element at index 4 = " + list[4]);
      Console.WriteLine("Element at index 5 = " + list[5]);
      Console.WriteLine("Element at index 6 = " + list[6]);
      Console.WriteLine("Element at index 7 = " + list[7]);
   }
}

実行結果

上記のコードを実行すると、次のような出力が得られます。

Element at index 0 = 100
Element at index 1 = 200
Element at index 2 = 300
Element at index 3 = 400
Element at index 4 = 500
Element at index 5 = 600
Element at index 6 = 700
Element at index 7 = 800

例2:指定インデックスの要素を設定(更新)する

次に、既存の要素を別の値に置き換える例を見てみましょう。インデクサーに新しい値を代入するだけで、該当位置の要素を更新できます。

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(String[] args) {
      List<String> list = new List<String>();
      list.Add("100");
      list.Add("200");
      list.Add("300");
      list.Add("400");
      list.Add("500");
      list.Add("600");
      list.Add("700");
      list.Add("800");
      list.Add("900");
      list.Add("1000");
      Console.WriteLine("Element at index 0 = " + list[0]);
      Console.WriteLine("Element at index 1 = " + list[1]);
      Console.WriteLine("Element at index 2 = " + list[2]);
      Console.WriteLine("Element at index 3 = " + list[3]);
      Console.WriteLine("Element at index 4 = " + list[4]);
      Console.WriteLine("Element at index 5 = " + list[5]);
      Console.WriteLine("Element at index 6 = " + list[6]);
      Console.WriteLine("Element at index 7 = " + list[7]);
      // インデックス3の要素を "450" に更新
      list[3] = "450";
      Console.WriteLine("Element at index 3 [UPDATED] = " + list[3]);
   }
}

実行結果

このコードを実行すると、次の出力が得られます。

Element at index 0 = 100
Element at index 1 = 200
Element at index 2 = 300
Element at index 3 = 400
Element at index 4 = 500
Element at index 5 = 600
Element at index 6 = 700
Element at index 7 = 800
Element at index 3 [UPDATED] = 450

ポイント

  • list[インデックス] の形式で読み取りも書き込みも可能です。
  • 存在しない範囲のインデックスを指定すると ArgumentOutOfRangeException がスローされるため、アクセス前に Count プロパティで範囲を確認すると安全です。
  1. Pythonでリストの最後の要素を取得する方法【初心者向け】

    Pythonでリストの最後の要素を取得する基本方法 Pythonでは、リストをはじめとするシーケンス型オブジェクトに対して、インデックス(添字)を使って任意の要素にアクセスできます。インデックスは0から始まるため、最初の要素は「0」、2番目の要素は「1」というように指定します。 ここで重要になるのが負のインデックスです。負の数を指定すると、リストの末尾から逆順に数えて要素を取得できます。したがって、リストの最後の要素を取得したい場合は、インデックスとして -1 を指定するだけでOKです。 >>> L1 = [1, 2, 3, 4, 5] >>> print(

  2. Pythonでリスト内の要素のインデックスを取得する方法

    index()メソッドで最初の出現位置を取得する リスト(文字列やタプルなど、他のシーケンス型でも同様)に用意されている index() メソッドを使うと、特定の要素が最初に出現する位置(インデックス)を簡単に見つけることができます。 >>> L1=[a, b, c, a, x] >>> L1 [a, b, c, a, x] >>> L1.index(a) 0 この例では、リスト L1 の中で a が最初に出現するのはインデックス 0 の位置であるため、0 が返されます。なお、指定した要素がリスト内に存在しない場合は ValueError