English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# SortedList(排序列表)

SortedList<TKey、TValue>およびSortedListは、関連のIComparer実装に基づいてキーでソートされたキー値ペアを格納できるコレクションクラスです。例えば、キーが原始型の場合は、キーの昇順にソートされます。

C#は泛型および非泛型のSortedListをサポートしています。泛型のSortedList<TKey, TValue>を使用することをお勧めします。それは非泛型のSortedListよりもより高速で、エラーが少ないです。

SortedListの特性

  • SortedList<TKey,TValue>はキーでソートされたキー値ペアの配列です。

  • 要素が追加された後すぐにソートされます。IComparer < T > に基づいて原始型のキーを昇順にソートし、オブジェクトのキーをソートします。

  • System.Collection.Generic ナミング空間に属しています

  • キーはユニークで、nullであってはなりません

  • 値はnullまたは重複することができます

  • インデクサーmySortedList[key]に関連するキーを渡して値にアクセスできます

  • KeyValuePair <TKey,TValue>の型の要素を含んでいます

  • SortedList<TKey,TValue>を使用するよりも少ないメモリを使用します。

  • ソートされたデータのリトリーブ速度が速く、SortedList<TKey,TValue>のキー値ペアの挿入および削除速度が速いです。

SortedListの作成

以下の例では、如何に泛型のSortedList<TKey, TValue>を作成し、キー値ペアを追加するかを示しています。

//整型のキーリスト、文字列の値 
SortedList<int, string> numberNames = new SortedList<int, string>();
numberNames.Add(3, "Three");
numberNames.Add(1, "One");
numberNames.Add(2, "Two");
numberNames.Add(4, null);
numberNames.Add(10, "Ten");
numberNames.Add(5, "Five");
//以下は例外を引き起こします
//numberNames.Add("Three", 3); //コンパイル時エラー:キーはint型でなければなりません
//numberNames.Add(1, "One"); //ランタイム例外:キーが重複しています
//numberNames.Add(null, "Five");//実行時例外:キーはnullではできません

上記の例では、TKeyとTValueの型を指定して、泛型のSortedList<TKey, TValue>オブジェクトが作成されます。SortedList<int, string>はint型のキーとstring型の値を格納します。

Add()メソッドは、SortedListに単一のキー値対を追加するために使用されます。キーはnullまたは重複できません。存在する場合、実行時例外が発生します。値は重複できます。型がnullになることができる場合、nullにすることができます。

SortedListインスタンスを初期化する際に、コレクションを使用します-初期化子文法を使用して、以下のように複数のキー値対を初期化します。

//文字列キーと文字列値のSortedListを作成 
//コレクションを使用して-初期化子文法
SortedList<string, string> cities = new SortedList<string, string>()
                                    {
                                        {"London", "UK"},
                                        {"New York", "USA"},
                                        {"Mumbai", "India"},
                                        {"Johannesburg", "South Africa"}
                                    };

SortedListにキー値対を追加後、キーに基づいてキー値対を再排序します。以下の例では、foreachループを使用してすべてのキーと値を表示します。

SortedList<int, string> numberNames = new SortedList<int, string>()
                                    {
                                        {3, "Three"},
                                        {5, "Five"},
                                        {1, "One"}
                                    };
Console.WriteLine("---初期のキー値--");
foreach(KeyValuePair<int, string> kvp in numberNames)
    Console.WriteLine("key: {0}, value: {{}}");1Console.WriteLine("key: {0}, value: {{}}", kvp.Key, kvp.Value);
numberNames.Add(6, "Six");
numberNames.Add(2, "Two");
numberNames.Add(4, "Four");
Console.WriteLine("---新しいキー値を追加後--");
foreach(var kvp in numberNames)
    Console.WriteLine("key: {0}, value: {{}}");1Console.WriteLine("key: {0}, value: {{}}", kvp.Key, kvp.Value);
出力:
---初期のキー値--
key: 1, value: One
key: 3, value: Three
key: 5, value: Five
---新しいキー値を追加後--
key: 1, value: One
key: 2, value: Two
key: 3, value: Three
key: 4, value: Four
key: 5, value: Five
key: 6, value: Six

SortedListにアクセス

インデクサー SortedList[key]でキーを指定して、SortedListの値を取得または設定します。

SortedList<int, string> numberNames = new SortedList<int, string>()
                                    {
                                        {3, "Three"},
                                        {1, "One"},
                                        {2, "Two"
                                    };
Console.WriteLine(numberNames[1]); //出力:One
Console.WriteLine(numberNames[2]); //出力:Two
Console.WriteLine(numberNames[3]); //出力:Three
//Console.WriteLine(numberNames[10]); //実行時、KeyNotFoundException
numberNames[2] = "TWO"; //値の更新
numberNames[4] = "Four"; //キーが存在しない場合、新しいキー値を追加します

上記、numberNames[1010

SortedList<int, string> numberNames = new SortedList<int, string>()
                                    {
                                        {3, "Three"},
                                        {1, "One"},
                                        {2, "Two"
                                    };
4)){
    numberNames[4] = "four";
}
int result;
if(numberNames.TryGetValue(4, out result))
    Console.WriteLine("Key: {0}, Value: {1} 4, result);
出力:
Key:4, Value: Four

Forループを使用してSortedListを迭代する場合は、KeysとValues属性を使用してください。

SortedList<int, string> numberNames = new SortedList<int, string>()
                                    {
                                        {3, "Three"},
                                        {1, "One"},
                                        {2, "Two"
                                    };
for (int i = 0; i < numberNames.Count;++)
{
    Console.WriteLine("key: {0}, value: {{}}");1}
}
出力:
key: 1, value: One
key: 2, value: Two
key: 3, value: Three

SortedListから要素を削除

Remove(key)とRemoveAt(index)メソッドを使用してSortedListからキー値ペアを削除します。

SortedList<int, string> numberNames = new SortedList<int, string>()
                                    {
                                        {3, "Three"},
                                        {1, "One"},
                                        {2, "Two"},
                                        {5, "Five"},
                                        {4, "Four"
                                    };
    
numberNames.Remove(1);//キーを削除1はい
numberNames.Remove(10);//キーを削除1はい、存在しない場合にはエラーは発生しません
numberNames.RemoveAt(0);//インデックス0からキー値ペアを削除 
//numberNames.RemoveAt(10);//実行時例外:ArgumentOutOfRangeException
foreach(var kvp in numberNames)
	Console.WriteLine("key: {0}, value: {{}}");1Console.WriteLine("key: {0}, value: {{}}", kvp.Key, kvp.Value);
出力:
key: 3, value: Three
key: 4, value: Four
key: 5, value: Five

SortedList クラス層

以下の図は SortedList 層の構造を示しています。

SortedList メソッドとプロパティの詳細については、docs.microsoft.comをご覧ください