次の方法で共有


SortedList.Add メソッド

指定したキーおよび値を持つ要素を SortedList に追加します。

Public Overridable Sub Add( _
   ByVal key As Object, _   ByVal value As Object _) Implements IDictionary.Add
[C#]
public virtual void Add(objectkey,objectvalue);
[C++]
public: virtual void Add(Object* key,Object* value);
[JScript]
public function Add(
   key : Object,value : Object);

パラメータ

  • key
    追加する要素のキー。
  • value
    追加する要素の値。値は null 参照 (Visual Basic では Nothing) に設定できます。

実装

IDictionary.Add

例外

例外の種類 条件
ArgumentNullException key が null 参照 (Visual Basic では Nothing) です。
ArgumentException 指定した key を持つ要素が、既に SortedList に存在します。

または

SortedListIComparable インターフェイスを使用するように設定されているのに、key が IComparable インターフェイスを実装していません。

NotSupportedException SortedList が読み取り専用です。

または

SortedList が固定サイズです。

InvalidOperationException 比較演算子が例外をスローしました。

解説

リストに追加された要素の数が現在の容量に達した場合、容量は自動的に 2 倍になります。要素の挿入位置は、 SortedList が作成されたときに明示的に選択された比較演算子か、既定の比較演算子に基づいて決定されます。

Item プロパティを使用すると、 SortedList 内に存在しないキーの値を設定することで、新しい要素を追加することもできます。たとえば、 myCollection["myNonexistentKey"] = myValue のように使用します。ただし、指定したキーが SortedList 内に既に存在する場合、 Item プロパティを設定すると既存の値が上書きされます。対照的に、 Add メソッドは既存の要素を変更しません。

SortedList の要素は、 SortedList が作成されるときに指定された IComparer の特定の実装か、キー自体が提供する IComparable 実装のいずれかに従って並べ替えられます。

キーには null 参照 (Visual Basic では Nothing) は使用できませんが、値は null でもかまいません。

使用例

[Visual Basic, C#, C++] 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("one", "The")
        mySL.Add("two", "quick")
        mySL.Add("three", "brown")
        mySL.Add("four", "fox")
        
        ' Displays the SortedList.
        Console.WriteLine("The SortedList contains the following:")
        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.
' 
' The SortedList contains the following:
'     -KEY-    -VALUE-
'     four:    fox
'     one:    The
'     three:    brown
'     two:    quick
 

[C#] 
using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( "one", "The" );
      mySL.Add( "two", "quick" );
      mySL.Add( "three", "brown" );
      mySL.Add( "four", "fox" );

      // Displays the SortedList.
      Console.WriteLine( "The SortedList contains the following:" );
      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.

The SortedList contains the following:
    -KEY-    -VALUE-
    four:    fox
    one:    The
    three:    brown
    two:    quick
*/

[C++] 

#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

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 __gc *mySL = new SortedList();
   mySL->Add( S"one", S"The" );
   mySL->Add( S"two", S"quick" );
   mySL->Add( S"three", S"brown" );
   mySL->Add( S"four", S"fox" );

   // Displays the SortedList.
   Console::WriteLine( "The SortedList contains the following:" );
   PrintKeysAndValues( mySL );
}

/* 
This code produces the following output.

The SortedList contains the following:
        -KEY-   -VALUE-
        four:   fox
        one:    The
        three:  brown
        two:    quick
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

SortedList クラス | SortedList メンバ | System.Collections 名前空間 | Item | IComparer | IComparable | Capacity