SortedList.Add メソッド
アセンブリ: System (system.dll 内)

Dim instance As SortedList(Of TKey, TValue) Dim key As TKey Dim value As TValue instance.Add(key, value)


並べ替えられたリストの値の型 TValue が参照型である場合、キーを null 参照 (Visual Basic では Nothing) にすることはできませんが、値を null 参照 (Visual Basic では Nothing) にすることはできます。
Item プロパティを使用すると、SortedList 内に存在しないキーの値を設定することで、新しい要素を追加することもできます (例 : myCollection["myNonexistentKey"] = myValue)。ただし、指定したキーが SortedList 内に既に存在する場合、Item プロパティを設定すると既存の値が上書きされます。対照的に、Add メソッドは既存の要素を変更しません。
Count が既に Capacity に等しい場合には、内部配列を自動的に再割り当てすることにより SortedList の容量が増加し、新しい要素を追加する前に既存の要素は新しい配列にコピーされます。
このメソッドは、並べ替えられていないデータに対する O(n) 操作です。ここで、n は Count です。新しい要素がリストの末尾に追加される場合は、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."); }

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


SortedList.Add メソッド
アセンブリ: mscorlib (mscorlib.dll 内)



要素の挿入位置は、SortedList が作成されたときに明示的に選択された比較演算子か、既定の比較演算子に基づいて決定されます。
Count が既に Capacity に等しい場合には、内部配列を自動的に再割り当てすることにより SortedList の容量が増加し、新しい要素を追加する前に既存の要素は新しい配列にコピーされます。
Item プロパティを使用すると、SortedList 内に存在しないキーの値を設定することで、新しい要素を追加することもできます (例 : myCollection["myNonexistentKey"] = myValue)。ただし、指定したキーが SortedList 内に既に存在する場合、Item プロパティを設定すると既存の値が上書きされます。対照的に、Add メソッドは既存の要素を変更しません。
SortedList の要素は、SortedList が作成されるときに指定された IComparer の特定の実装か、キー自体が提供する IComparable 実装のいずれかに従って並べ替えられます。
キーには null 参照 (Visual Basic では Nothing) は使用できませんが、値は null でもかまいません。
このメソッドは、並べ替えられていないデータに対する O(n) 操作です。ここで、n は Count です。新しい要素がリストの末尾に追加される場合は、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 */

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.Addを検索する場合は、下記のリンクをクリックしてください。

- SortedList.Addのページへのリンク