SortedList.Addとは? わかりやすく解説

SortedList.Add メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

Public Sub Add ( _
    key As TKey, _
    value As TValue _
)
Dim instance As SortedList(Of
 TKey, TValue)
Dim key As TKey
Dim value As TValue

instance.Add(key, value)
public void Add (
    TKey key,
    TValue value
)
public:
virtual void Add (
    TKey key, 
    TValue value
) sealed
public final void Add (
    TKey key, 
    TValue value
)
public final function Add (
    key : TKey, 
    value : TValue
)

パラメータ

key

追加する要素キー

value

追加する要素の値。参照型場合は、値を null 参照 (Visual Basic では Nothing) にできます

例外例外
例外種類条件

ArgumentNullException

keynull 参照 (Visual Basic では Nothing) です。

ArgumentException

同じキー持つ要素が、SortedList に既に存在します

解説解説

並べ替えられたリストの値の型 TValue参照型である場合キーnull 参照 (Visual Basic では Nothing) にすることはできませんが、値を null 参照 (Visual Basic では Nothing) にすることはできます

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

Count が既に Capacity等し場合には、内部配列自動的に割り当てすることにより SortedList容量増加し新し要素追加する前に既存要素新し配列コピーされます。

このメソッドは、並べ替えられていないデータ対する O(n) 操作です。ここで、nCount です。新し要素リスト末尾追加される場合は、O(log n) 操作です。挿入によってサイズ変更発生する場合操作は O(n) になります

使用例使用例

文字列キーを含む文字列の空の SortedList作成しAdd メソッド使用していくつかの要素追加するコード例次に示します。この例では、重複するキー追加しようとすると、Add メソッドArgumentExceptionスローすることを示します

このコード例は、SortedList クラストピック取り上げているコード例一部分です。

' Create a new sorted list of strings, with string 
' keys. 
Dim openWith As New SortedList(Of
 String, String)

' Add some elements to the list. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the list.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt""
 already exists.")
End Try
// Create a new sorted list of strings, with string
// keys.
SortedList<string, string> openWith =
 
    new SortedList<string, string>();

// Add some elements to the list. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is 
// already in the list.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SortedList ジェネリック クラス
SortedList メンバ
System.Collections.Generic 名前空間
Remove
Item
IDictionary.Add メソッド

SortedList.Add メソッド

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

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

keynull 参照 (Visual Basic では Nothing) です。

ArgumentException

指定した key持つ要素が、既に SortedList存在します

または

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

NotSupportedException

SortedList読み取り専用です。

または

SortedList固定サイズです。

InvalidOperationException

比較演算子例外スローしました

解説解説

要素挿入位置は、SortedList作成されたときに明示的に選択され比較演算子か、既定比較演算子基づいて決定されます。

Count が既に Capacity等し場合には、内部配列自動的に割り当てすることにより SortedList容量増加し新し要素追加する前に既存要素新し配列コピーされます。

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

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

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

このメソッドは、並べ替えられていないデータ対する O(n) 操作です。ここで、nCount です。新し要素リスト末尾追加される場合は、O(log n) 操作です。挿入によってサイズ変更発生する場合操作は O(n) になります

使用例使用例

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
 
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
*/
#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^ mySL = gcnew 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 );
}

/* 
This code produces the following output.

The SortedList contains the following:
        -KEY-   -VALUE-
        four:   fox
        one:    The
        three:  brown
        two:    quick
*/
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("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);
    } //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.
 
 The SortedList contains the following:
     -KEY-    -VALUE-
     four:    fox
     one:    The
     three:    brown
     two:    quick
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


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

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

辞書ショートカット

すべての辞書の索引

「SortedList.Add」の関連用語

SortedList.Addのお隣キーワード
検索ランキング

   

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



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

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

©2025 GRAS Group, Inc.RSS