Hashtable.Add メソッドとは? わかりやすく解説

Hashtable.Add メソッド

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

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

例外例外
例外種類条件

ArgumentNullException

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

ArgumentException

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

NotSupportedException

Hashtable読み取り専用です。

または

Hashtable固定サイズです。

解説解説

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

態とハッシュ コード値との間に相関関係のないオブジェクトは、通常キーとして使用しないください。たとえば、StringBuilder オブジェクトよりも String オブジェクトの方が、キーとして使用するには適してます。

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

CountHashtable容量よりも小さ場合、このメソッドは O(1) 操作なります新し要素格納するために容量増やす必要がある場合、このメソッドは O(n) 操作なります。ここで、nCount です。

使用例使用例

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 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


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

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

辞書ショートカット

すべての辞書の索引

「Hashtable.Add メソッド」の関連用語

Hashtable.Add メソッドのお隣キーワード
検索ランキング

   

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



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

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

©2025 GRAS Group, Inc.RSS