SortedList クラスとは? わかりやすく解説

SortedList クラス

キーによって並べ替えられ、キーインデックス使ってアクセスできる、キー/値ペアコレクション表します

名前空間: System.Collections
アセンブリ: 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
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class SortedList implements IDictionary,
 ICollection, 
    IEnumerable, ICloneable
SerializableAttribute 
ComVisibleAttribute(true) 
public class SortedList implements 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.Object
  System.Collections.SortedList
スレッド セーフスレッド セーフ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からSortedList クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からSortedList クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からSortedList クラス を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「SortedList クラス」の関連用語

SortedList クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SortedList クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS