Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ リストとは何ですか? | データ構造とファイル操作
C#オブジェクト指向構造

bookリストとは何ですか?

メニューを表示するにはスワイプしてください

Prerequisites
前提条件

特定のコースを受講しているすべての学生の名前を保存したい状況を想像してください。最初に思いつく解決策は、配列を作成することです。

index.cs

index.cs

copy
123456
using System; class Program { string[] studentNames = new string[50]; }

しかし、もし学生が50人を超える場合、その名前を格納することができません。逆に、学生が50人未満の場合、配列内の未使用スペースはメモリの無駄となります。特に大きな配列ではこの問題が顕著になります。

ここで、可変数の要素を格納できる新しい構造が必要となります。幸いにも、そのような構造はすでに存在しており、これを**リスト(List)**と呼びます。

**リスト(List)**は配列と非常によく似ていますが、リストに格納される要素数は変更可能です。

Note
ノート

配列内の既存要素を変更することは可能ですが、新しい要素を追加することはできません。

空のリストを宣言するための構文は次のとおりです。

この構文を使用して、学生の名前を格納するリストを作成可能。

index.cs

index.cs

copy
12345678910
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { List<string> students = new List<string>(); } }

リストを使用するには、Generic モジュールをインポートする必要がある点に注意。

index.cs

index.cs

copy
1
using System.Collections.Generic;

必要なモジュールをインポートするには、using System;の下にこの行を追加します。

Add() メソッド

Add メソッドを使用してリストに要素を追加できます:

index.cs

index.cs

copy
123456789101112131415
using 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

index.cs

copy
1234567891011121314151617
using 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

index.cs

copy
123456789101112131415161718
using 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

index.cs

copy
1234567891011121314151617181920
using 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

index.cs

copy
12345678910111213141516
using 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

index.cs

copy
123456789101112131415
using 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

index.cs

copy
123456789101112131415
using 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); } } }
Note
ヒント:

宣言構文を短くするために、暗黙的宣言を使用することも可能。明示的宣言とは、変数宣言時にデータ型を指定する方法。

index.cs

index.cs

copy
1
float number = 7.9f;

一方、暗黙的宣言では var キーワードを使用するだけで、コンパイラが代入された値に基づいて変数のデータ型を自動的に推論。

index.cs

index.cs

copy
1
var number = 7.9f;

リストを宣言する際にも暗黙的な宣言を使用可能:

index.cs

index.cs

copy
1
var numbers = new List<int> { 1, 2, 3, 4, 5 };

1. 次のコードの出力は何ですか:

2. リストを使用するためにインポートが必要なモジュールはどれですか?

3. リストのサイズ(長さ)を取得するために使用されるメソッドはどれですか?

question mark

次のコードの出力は何ですか:

正しい答えを選んでください

question mark

リストを使用するためにインポートが必要なモジュールはどれですか?

正しい答えを選んでください

question mark

リストのサイズ(長さ)を取得するために使用されるメソッドはどれですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  1

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  1
some-alt