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



キーには null 参照 (Visual Basic では Nothing) は使用できませんが、値は null でもかまいません。
状態とハッシュ コード値との間に相関関係のないオブジェクトは、通常はキーとして使用しないでください。たとえば、StringBuilder オブジェクトよりも String オブジェクトの方が、キーとして使用するには適しています。
Item プロパティを使用すると、Hashtable 内に存在しないキーの値を設定することで、新しい要素を追加することもできます (例 : myCollection["myNonexistentKey"] = myValue)。ただし、指定したキーが Hashtable 内に既に存在する場合、Item プロパティを設定すると既存の値が上書きされます。対照的に、Add メソッドは既存の要素を変更しません。
Count が Hashtable の容量よりも小さい場合、このメソッドは O(1) 操作になります。新しい要素を格納するために容量を増やす必要がある場合、このメソッドは O(n) 操作になります。ここで、n は Count です。

Hashtable に要素を追加する方法の例を次に示します。
Imports System Imports System.Collections Public Class SamplesHashtable Public Shared Sub Main() ' Creates and initializes a new Hashtable. Dim myHT As New Hashtable() myHT.Add("one", "The") myHT.Add("two", "quick") myHT.Add("three", "brown") myHT.Add("four", "fox") ' Displays the Hashtable. Console.WriteLine("The Hashtable contains the following:") PrintKeysAndValues(myHT) End Sub 'Main Public Shared Sub PrintKeysAndValues(myHT As Hashtable) Console.WriteLine(vbTab + "-KEY-" + vbTab + "-VALUE-") Dim de As DictionaryEntry For Each de In myHT Console.WriteLine(vbTab + "{0}:" + vbTab + "{1}", de.Key, de.Value) Next de Console.WriteLine() End Sub 'PrintKeysAndValues End Class 'SamplesHashtable ' This code produces the following output. ' ' The Hashtable contains the following: ' -KEY- -VALUE- ' two: quick ' three: brown ' four: fox ' one: The '
using System; using System.Collections; public class SamplesHashtable { public static void Main() { // Creates and initializes a new Hashtable. Hashtable myHT = new Hashtable(); myHT.Add( "one", "The" ); myHT.Add( "two", "quick" ); myHT.Add( "three", "brown" ); myHT.Add( "four", "fox" ); // Displays the Hashtable. Console.WriteLine( "The Hashtable contains the following:" ); PrintKeysAndValues( myHT ); } public static void PrintKeysAndValues( Hashtable myHT ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); foreach ( DictionaryEntry de in myHT ) Console.WriteLine( "\t{0}:\t{1}", de.Key, de.Value ); Console.WriteLine(); } } /* This code produces the following output. The Hashtable contains the following: -KEY- -VALUE- two: quick three: brown four: fox one: The */
using namespace System; using namespace System::Collections; void PrintKeysAndValues( Hashtable^ myHT ); int main() { // Creates and initializes a new Hashtable. Hashtable^ myHT = gcnew Hashtable; myHT->Add( "one", "The" ); myHT->Add( "two", "quick" ); myHT->Add( "three", "brown" ); myHT->Add( "four", "fox" ); // Displays the Hashtable. Console::WriteLine( "The Hashtable contains the following:" ); PrintKeysAndValues( myHT ); } void PrintKeysAndValues( Hashtable^ myHT ) { Console::WriteLine( "\t-KEY-\t-VALUE-" ); IEnumerator^ myEnum = myHT->GetEnumerator(); while ( myEnum->MoveNext() ) { DictionaryEntry de = *safe_cast<DictionaryEntry ^>(myEnum->Current); Console::WriteLine( "\t{0}:\t{1}", de.Key, de.Value ); } Console::WriteLine(); } /* This code produces the following output. The Hashtable contains the following: -KEY- -VALUE- two: quick three: brown four: fox one: The */
import System.*; import System.Collections.*; public class SamplesHashtable { public static void main(String[] args) { // Creates and initializes a new Hashtable. Hashtable myHT = new Hashtable(); myHT.Add("one", "The"); myHT.Add("two", "quick"); myHT.Add("three", "brown"); myHT.Add("four", "fox"); // Displays the Hashtable. Console.WriteLine("The Hashtable contains the following:"); PrintKeysAndValues(myHT); } //main public static void PrintKeysAndValues(Hashtable myHT) { Console.WriteLine("\t-KEY-\t-VALUE-"); IEnumerator myEnumerator = myHT.GetEnumerator(); while (myEnumerator.MoveNext()) { DictionaryEntry de = (DictionaryEntry)myEnumerator.get_Current(); Console.WriteLine("\t{0}:\t{1}", de.get_Key(), de.get_Value()); } Console.WriteLine(); } //PrintKeysAndValues } //SamplesHashtable /* This code produces the following output. The Hashtable contains the following: -KEY- -VALUE- two: quick three: brown four: fox one: The */
import System import System.Collections // Creates and initializes a new Hashtable. var myHT : Hashtable = new Hashtable() myHT.Add("one", "The") myHT.Add("two", "quick") myHT.Add("three", "brown") myHT.Add("four", "fox") // Displays the Hashtable. Console.WriteLine("The Hashtable contains the following:") PrintKeysAndValues(myHT) function PrintKeysAndValues(myList : Hashtable){ var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator() Console.WriteLine("\t-KEY-\t-VALUE-") while(myEnumerator.MoveNext()) Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value) Console.WriteLine() } // This code produces the following output. // // The Hashtable contains the following: // -KEY- -VALUE- // three: brown // four: fox // two: quick // one: The

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に収録されているすべての辞書からHashtable.Add メソッドを検索する場合は、下記のリンクをクリックしてください。

- Hashtable.Add メソッドのページへのリンク