【C#入門】SortedListオブジェクトからキーのリストを取得する方法(GetKeyListメソッド)
C#のSortedListオブジェクトに格納されているキーの一覧を取得するには、GetKeyList()メソッドを使用します。このメソッドは、SortedList内のすべてのキーをIList型のコレクションとして返します。
GetKeyList()メソッドの基本構文
public virtual System.Collections.IList GetKeyList ();
戻り値は、SortedListのすべてのキーを含むIListオブジェクトです。これは読み取り専用のビューであり、返されたリストへの変更は元のSortedListには反映されません。
サンプルコード1:複数のSortedListからキーを取得する
以下の例では、2つの異なるSortedListを作成し、それぞれのキーのリストをGetKeyList()メソッドで取得して表示しています。
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList list1 = new SortedList();
list1.Add("One", 1);
list1.Add("Two", 2);
list1.Add("Three", 3);
list1.Add("Four", 4);
list1.Add("Five", 5);
list1.Add("Six", 6);
list1.Add("Seven", 7);
list1.Add("Eight", 8);
list1.Add("Nine", 9);
list1.Add("Ten", 10);
Console.WriteLine("SortedList1 の要素...");
foreach(DictionaryEntry d in list1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("
キーのリスト...SortedList1");
IList list = list1.GetKeyList();
foreach(string res in list)
Console.WriteLine(res);
SortedList list2 = new SortedList();
list2.Add("A", "Accessories");
list2.Add("B", "Books");
list2.Add("C", "Smart Wearable Tech");
list2.Add("D", "Home Appliances");
Console.WriteLine("
SortedList2 の要素...");
foreach(DictionaryEntry d in list2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("
キーのリスト...SortedList2");
list = list2.GetKeyList();
foreach(string res in list)
Console.WriteLine(res);
}
}実行結果
上記のコードを実行すると、次のような出力が得られます。SortedListはキーで自動的にソートされるため、要素とキーのリストはどちらも昇順に並んでいる点に注目してください。
SortedList1 の要素... Eight 8 Five 5 Four 4 Nine 9 One 1 Seven 7 Six 6 Ten 10 Three 3 Two 2 キーのリスト...SortedList1 Eight Five Four Nine One Seven Six Ten Three Two SortedList2 の要素... A Accessories B Books C Smart Wearable Tech D Home Appliances キーのリスト...SortedList2 A B C D
サンプルコード2:シンプルな例
もう少しシンプルな例を見てみましょう。数値を値として持つSortedListを作成し、そのキーの一覧を取得します。
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList list = new SortedList();
list.Add("One", 1);
list.Add("Two", 2);
list.Add("Three", 3);
list.Add("Four", 4);
list.Add("Five", 5);
Console.WriteLine("SortedList の要素...");
foreach(DictionaryEntry d in list) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("
キーのリスト...SortedList");
IList col = list.GetKeyList();
foreach(string res in col)
Console.WriteLine(res);
}
}実行結果
SortedList の要素... Five 5 Four 4 One 1 Three 3 Two 2 キーのリスト...SortedList Five Four One Three Two
ポイントまとめ
- GetKeyList()を使うと、SortedListの全キーを
IListとして一括取得できます。 - 同様に、値の一覧を取得したい場合はGetValueList()メソッドを使用します。
- 個別のキーにアクセスする場合はGetKey(int index)メソッドも利用可能です。
- SortedListは挿入順ではなく、キーに基づいて自動的にソートされるため、取得したキーリストもソート済みの順序になります。
-
C#のSortedListクラスのValuesプロパティとは?使い方を実例付きで解説
C#のSortedListクラスのValuesプロパティとは C#のSortedListクラスには、コレクションに格納されたすべての値(バリュー)を取得できるValuesプロパティが用意されています。キーを意識せずに値だけを順番に取り出したい場合に便利なプロパティです。ここでは、実際のコード例を通して基本的な使い方を解説します。 手順1:SortedListクラスのインスタンスを作成する まず、SortedListクラスを宣言します。 SortedList list = new SortedList(); 手順2:Addメソッドで要素を追加する 続いて、Addメソッドを使ってキーと値のペアを追
-
【C#】SortedListクラスのCountプロパティとは?使い方を実例で解説
Countプロパティとは C#のSortedListクラスは、キーに基づいて自動的にソートされるキーと値のペアを管理するコレクションです。このクラスが持つCountプロパティを使うと、コレクション内に実際に格納されている要素(キーと値のペア)の数を取得できます。 Countプロパティは読み取り専用で、戻り値はint型です。なお、コレクションの内部容量を表すCapacityプロパティとは異なり、Countは「実際に追加されている要素数」を返すという点に注意してください。 Countプロパティの主な特徴 SortedListに格納されている要素の総数をint型で返す 読み取り専用のため、外部か