SortedList クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public Class SortedList Implements IDictionary, ICollection, IEnumerable, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public class SortedList : IDictionary, ICollection, IEnumerable, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class SortedList : IDictionary, ICollection, IEnumerable, ICloneable

このコレクションのジェネリック バージョンについては、「System.Collections.Generic.SortedList」を参照してください。
SortedList 要素にアクセスするには、IDictionary 実装内の要素のようにキーを使用するか、IList 実装内の要素のようにインデックスを使用します。
SortedList は、リストの要素を格納するために、内部に 2 つの配列を保持しています。一方の配列にはキーを格納し、他方の配列にはキーに関連付けられている値を格納します。各要素はキー/値ペアであり、DictionaryEntry オブジェクトとしてアクセスできます。キーには null 参照 (Visual Basic では Nothing) は使用できませんが、値は null でもかまいません。
SortedList の容量は、SortedList が保持できる要素数になります。SortedList の既定の初期量は 0 です。SortedList に要素を追加すると、必要に応じて、再割り当てによって容量が自動的に増加します。容量を減らすには、TrimToSize を呼び出すか、Capacity プロパティを明示的に設定します。
SortedList の要素は、SortedList が作成されるときに指定された IComparer の特定の実装か、キー自体が提供する IComparable 実装のいずれかに従って並べ替えられます。いずれの場合でも、SortedList では、キーが重複することはあり得ません。
並べ替え順に基づいたインデックス順。要素を追加すると、その要素は正しい並べ替え順に従って SortedList に挿入され、それに応じてインデックスも調整されます。要素が削除されると、それに応じてインデックスも調整されます。したがって、特定のキー/値ペアのインデックスは、要素が SortedList で追加または削除されるときに変更されることがあります。
SortedList に対する操作は、並べ替えが必要になるため、Hashtable に対する操作よりも遅くなる傾向があります。ただし、SortedList では、関連付けられているキーまたはインデックスのどちらを使用しても値にアクセスできるため、柔軟性という面ではより優れています。
このコレクション内の要素は、整数インデックスを使用してアクセスできます。このコレクションのインデックスは 0 から始まります。
C# 言語の foreach ステートメント (Visual Basic では for each) は、コレクション内の各要素の型を必要とします。SortedList の各要素はキー/値ペアであるため、要素の型は、キーの型や値の型にはなりません。その代わり、要素の型は DictionaryEntry になります。例 :
foreach (DictionaryEntry de in mySortedList) {...}
For Each de As DictionaryEntry In mySortedList ... Next myDE
foreach ステートメントは、列挙子のラッパーです。これは、コレクションからの読み取りだけを許可し、コレクションへの書き込みを防ぎます。

SortedList を作成および初期化する方法と、そのキーと値を出力する方法の例を次に示します。
Imports System Imports System.Collections Imports Microsoft.VisualBasic Public Class SamplesSortedList Public Shared Sub Main() ' Creates and initializes a new SortedList. Dim mySL As New SortedList() mySL.Add("First", "Hello") mySL.Add("Second", "World") mySL.Add("Third", "!") ' Displays the properties and values of the SortedList. Console.WriteLine("mySL") Console.WriteLine(" Count: {0}", mySL.Count) Console.WriteLine(" Capacity: {0}", mySL.Capacity) Console.WriteLine(" Keys and Values:") PrintKeysAndValues(mySL) End Sub Public Shared Sub PrintKeysAndValues(myList As SortedList) Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _ "-VALUE-") Dim i As Integer For i = 0 To myList.Count - 1 Console.WriteLine(ControlChars.Tab & "{0}:" & ControlChars.Tab & _ "{1}", myList.GetKey(i), myList.GetByIndex(i)) Next i Console.WriteLine() End Sub End Class ' This code produces the following output. ' ' mySL ' Count: 3 ' Capacity: 16 ' Keys and Values: ' -KEY- -VALUE- ' First: Hello ' Second: World ' Third: !
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add("First", "Hello"); mySL.Add("Second", "World"); mySL.Add("Third", "!"); // Displays the properties and values of the SortedList. Console.WriteLine( "mySL" ); Console.WriteLine( " Count: {0}", mySL.Count ); Console.WriteLine( " Capacity: {0}", mySL.Capacity ); Console.WriteLine( " Keys and Values:" ); PrintKeysAndValues( mySL ); } public static void PrintKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. mySL Count: 3 Capacity: 16 Keys and Values: -KEY- -VALUE- First: Hello Second: World Third: ! */
#using <system.dll> using namespace System; using namespace System::Collections; public ref class SamplesSortedList { public: static void PrintKeysAndValues( SortedList^ myList ) { Console::WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList->Count; i++ ) { Console::WriteLine( "\t{0}:\t{1}", myList->GetKey( i ), myList->GetByIndex( i ) ); } Console::WriteLine(); } }; int main() { // Creates and initializes a new SortedList. SortedList^ mySL = gcnew SortedList; mySL->Add( "First", "Hello" ); mySL->Add( "Second", "World" ); mySL->Add( "Third", "!" ); // Displays the properties and values of the SortedList. Console::WriteLine( "mySL" ); Console::WriteLine( " Count: {0}", mySL->Count ); Console::WriteLine( " Capacity: {0}", mySL->Capacity ); Console::WriteLine( " Keys and Values:" ); SamplesSortedList::PrintKeysAndValues( mySL ); } /* This code produces the following output. mySL Count: 3 Capacity: 16 Keys and Values: -KEY- -VALUE- First: Hello Second: World Third: ! */
import System.*; import System.Collections.*; public class SamplesSortedList { public static void main(String[] args) { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add("First", "Hello"); mySL.Add("Second", "World"); mySL.Add("Third", "!"); // Displays the properties and values of the SortedList. Console.WriteLine("mySL"); Console.WriteLine(" Count: {0}", System.Convert.ToString(mySL.get_Count())); Console.WriteLine(" Capacity: {0}", System.Convert.ToString(mySL.get_Capacity())); Console.WriteLine(" Keys and Values:"); PrintKeysAndValues(mySL); } //main public static void PrintKeysAndValues(SortedList myList) { Console.WriteLine("\t-KEY-\t-VALUE-"); for (int i = 0; i < myList.get_Count(); i++) { Console.WriteLine("\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i)); } Console.WriteLine(); } //PrintKeysAndValues } //SamplesSortedList /* This code produces the following output. mySL Count: 3 Capacity: 16 Keys and Values: -KEY- -VALUE- First: Hello Second: World Third: ! */

System.Collections.SortedList

この型の public static (Visual Basic では Shared) メンバは、スレッド セーフです。すべてのインスタンス メンバがスレッド セーフになるかどうかは保証されていません。
コレクションが変更されない限り、SortedList では、複数の読み込み操作が同時に発生しても問題ありません。SortedList を確実にスレッド セーフにするためには、すべての操作を Synchronized メソッドから返されるラッパー経由で実行する必要があります。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からSortedList クラスを検索する場合は、下記のリンクをクリックしてください。

- SortedList クラスのページへのリンク