リストとは何ですか?
メニューを表示するにはスワイプしてください
特定のコースを受講しているすべての学生の名前を保存したい状況を想像してください。最初に思いつく解決策は、配列を作成することです。
index.cs
123456using System; class Program { string[] studentNames = new string[50]; }
しかし、もし学生が50人を超える場合、その名前を格納することができません。逆に、学生が50人未満の場合、配列内の未使用スペースはメモリの無駄となります。特に大きな配列ではこの問題が顕著になります。
ここで、可変数の要素を格納できる新しい構造が必要となります。幸いにも、そのような構造はすでに存在しており、これを**リスト(List)**と呼びます。
**リスト(List)**は配列と非常によく似ていますが、リストに格納される要素数は変更可能です。
配列内の既存要素を変更することは可能ですが、新しい要素を追加することはできません。
空のリストを宣言するための構文は次のとおりです。
この構文を使用して、学生の名前を格納するリストを作成可能。
index.cs
12345678910using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List<string> students = new List<string>(); } }
リストを使用するには、Generic モジュールをインポートする必要がある点に注意。
index.cs
1using System.Collections.Generic;
必要なモジュールをインポートするには、using System;の下にこの行を追加します。
Add() メソッド
Add メソッドを使用してリストに要素を追加できます:
index.cs
123456789101112131415using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List<string> students = new List<string>(); students.Add("Anna"); students.Add("Laura"); students.Add("Jacob"); students.Add("Aron"); } }
上記のコードは、students リストに4つの要素を追加します。
string 型の配列が string 要素のみを含むことができるのと同様に、string 型のリストも string 要素のみを受け入れます。
インデックス
最初の要素は Anna であり、インデックスは 0 になります。一方、Laura はインデックス 1 となります。このように、リストの要素は配列と同様にインデックスを使ってアクセスできます:
index.cs
1234567891011121314151617using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List<string> students = new List<string>(); students.Add("Anna"); students.Add("Laura"); students.Add("Jacob"); students.Add("Aron"); Console.WriteLine(students[2]); } }
Count() メソッド
リストの長さは、その Count 属性を使用して取得可能。
index.cs
123456789101112131415161718using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List<string> students = new List<string>(); students.Add("Anna"); students.Add("Laura"); students.Add("Jacob"); students.Add("Aron"); Console.WriteLine(students[2]); Console.WriteLine(students.Count); } }
動的な長さ
リストの長さは動的(変更可能)であり、要素を追加するたびに変化することに注意。
index.cs
1234567891011121314151617181920using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List<string> students = new List<string>(); Console.WriteLine(students.Count); students.Add("Anna"); Console.WriteLine(students.Count); students.Add("Laura"); Console.WriteLine(students.Count); students.Add("Jacob"); Console.WriteLine(students.Count); students.Add("Aron"); Console.WriteLine(students.Count); } }
初期化
次の構文を使用して、いくつかの要素でリストを初期化することも可能。
List<dataType> listName = new List<dataType>{ element1, element2, … };
index.cs
12345678910111213141516using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List<string> students = new List<string> { "Anna", "Laura", "Jacob", "Aron" }; // It is still possible to add additional elements after initialization students.Add("Markus"); Console.WriteLine(students.Count); Console.WriteLine(students[4]); } }
リストのループ処理
リストを配列と同様にループ処理
forループの使用
index.cs
123456789101112131415using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List<string> students = new List<string> { "Anna", "Laura", "Jacob", "Aron" }; for (int i = 0; i < students.Count; i++) { Console.WriteLine(students[i]); } } }
foreach ループの使用
index.cs
123456789101112131415using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List<string> students = new List<string> { "Anna", "Laura", "Jacob", "Aron" }; foreach (string studentName in students) { Console.WriteLine(studentName); } } }
宣言構文を短くするために、暗黙的宣言を使用することも可能。明示的宣言とは、変数宣言時にデータ型を指定する方法。
index.cs
1float number = 7.9f;
一方、暗黙的宣言では var キーワードを使用するだけで、コンパイラが代入された値に基づいて変数のデータ型を自動的に推論。
index.cs
1var number = 7.9f;
リストを宣言する際にも暗黙的な宣言を使用可能:
index.cs
1var numbers = new List<int> { 1, 2, 3, 4, 5 };
1. 次のコードの出力は何ですか:
2. リストを使用するためにインポートが必要なモジュールはどれですか?
3. リストのサイズ(長さ)を取得するために使用されるメソッドはどれですか?
フィードバックありがとうございます!
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください