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

Hashtable.CopyTo メソッド

1 次元Array インスタンス指定したインデックスHashtable要素コピーします

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

Public Overridable Sub CopyTo
 ( _
    array As Array, _
    arrayIndex As Integer _
)
Dim instance As Hashtable
Dim array As Array
Dim arrayIndex As Integer

instance.CopyTo(array, arrayIndex)
public void CopyTo (
    Array array, 
    int arrayIndex
)

パラメータ

array

Hashtable から DictionaryEntry オブジェクトコピーされる 1 次元ArrayArray には、0 から始まるインデックス番号が必要です。

arrayIndex

コピー開始位置となる、array の 0 から始まるインデックス番号

例外例外
例外種類条件

ArgumentNullException

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

ArgumentOutOfRangeException

arrayIndex が 0 未満です。

ArgumentException

array多次元です。

または

arrayIndexarray長さ上です。

または

コピー元の Hashtable要素数が、arrayIndex からコピー先の array末尾までに格納できる数を超えてます。

InvalidCastException

コピー元の Hashtable の型が、コピー先の array の型に自動的にキャストできません。

解説解説

要素は、列挙子が Hashtable反復処理するのと同じ順序で、Arrayコピーされます。

Hashtable 内のキーだけをコピーするには、Hashtable.Keys.CopyTo使用します

Hashtable 内の値だけをコピーするには、Hashtable.Values.CopyTo使用します

このメソッドは O(n) 操作です。ここで、nCount です。

使用例使用例

1 次元ArrayHashtable 内のキーまたは値のリストコピーする方法の例を次に示します

Imports System
Imports System.Collections

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Creates and initializes the source Hashtable.
        Dim mySourceHT As New
 Hashtable()
        mySourceHT.Add("A", "valueA")
        mySourceHT.Add("B", "valueB")

        ' Creates and initializes the one-dimensional target Array.
        Dim myTargetArray(14) As String
        myTargetArray(0) = "The"
        myTargetArray(1) = "quick"
        myTargetArray(2) = "brown"
        myTargetArray(3) = "fox"
        myTargetArray(4) = "jumped"
        myTargetArray(5) = "over"
        myTargetArray(6) = "the"
        myTargetArray(7) = "lazy"
        myTargetArray(8) = "dog"

        ' Displays the values of the target Array.
        Console.WriteLine("The target Array contains the following
 before:")
        PrintValues(myTargetArray, " "c)

        ' Copies the keys in the source Hashtable to the target Hashtable,
 starting at index 6.
        Console.WriteLine("After copying the keys, starting at
 index 6:")
        mySourceHT.Keys.CopyTo(myTargetArray, 6)

        ' Displays the values of the target Array.
        PrintValues(myTargetArray, " "c)

        ' Copies the values in the source Hashtable to the target Hashtable,
 starting at index 6.
        Console.WriteLine("After copying the values, starting
 at index 6:")
        mySourceHT.Values.CopyTo(myTargetArray, 6)

        ' Displays the values of the target Array.
        PrintValues(myTargetArray, " "c)

    End Sub 'Main

    Public Shared Sub PrintValues(myArr()
 As String, mySeparator As
 Char)
        Dim i As Integer
        For i = 0 To myArr.Length - 1
            Console.Write("{0}{1}", mySeparator, myArr(i))
        Next i
        Console.WriteLine()
    End Sub 'PrintValues

End Class 'SamplesHashtable


' This code produces the following output.
' 
' The target Array contains the following before:
'  The quick brown fox jumped over the lazy dog
' After copying the keys, starting at index 6:
'  The quick brown fox jumped over B A dog
' After copying the values, starting at index 6:
'  The quick brown fox jumped over valueB valueA dog

using System;
using System.Collections;
public class SamplesHashtable  {

   public static void Main()
  {

      // Creates and initializes the source Hashtable.
      Hashtable mySourceHT = new Hashtable();
      mySourceHT.Add( "A", "valueA" );
      mySourceHT.Add( "B", "valueB" );

      // Creates and initializes the one-dimensional target Array.
      String[] myTargetArray = new String[15];
      myTargetArray[0] = "The";
      myTargetArray[1] = "quick";
      myTargetArray[2] = "brown";
      myTargetArray[3] = "fox";
      myTargetArray[4] = "jumped";
      myTargetArray[5] = "over";
      myTargetArray[6] = "the";
      myTargetArray[7] = "lazy";
      myTargetArray[8] = "dog";

      // Displays the values of the target Array.
      Console.WriteLine( "The target Array contains the following before:"
 );
      PrintValues( myTargetArray, ' ' );

      // Copies the keys in the source Hashtable to the target Hashtable,
 starting at index 6.
      Console.WriteLine( "After copying the keys, starting at index 6:"
 );
      mySourceHT.Keys.CopyTo( myTargetArray, 6 );

      // Displays the values of the target Array.
      PrintValues( myTargetArray, ' ' );

      // Copies the values in the source Hashtable to the target Hashtable,
 starting at index 6.
      Console.WriteLine( "After copying the values, starting at index 6:"
 );
      mySourceHT.Values.CopyTo( myTargetArray, 6 );

      // Displays the values of the target Array.
      PrintValues( myTargetArray, ' ' );
   }

   public static void PrintValues(
 String[] myArr, char mySeparator )  {
      for ( int i = 0; i < myArr.Length;
 i++ )
         Console.Write( "{0}{1}", mySeparator, myArr[i] );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The target Array contains the following before:
 The quick brown fox jumped over the lazy dog
After copying the keys, starting at index 6:
 The quick brown fox jumped over B A dog
After copying the values, starting at index 6:
 The quick brown fox jumped over valueB valueA dog

*/ 
using namespace System;
using namespace System::Collections;
void PrintValues( array<String^>^myArr, char
 mySeparator );
int main()
{
   
   // Creates and initializes the source Hashtable.
   Hashtable^ mySourceHT = gcnew Hashtable;
   mySourceHT->Add( "A", "valueA" );
   mySourceHT->Add( "B", "valueB" );
   
   // Creates and initializes the one-dimensional target Array.
   array<String^>^myTargetArray = gcnew array<String^>(15);
   myTargetArray[ 0 ] = "The";
   myTargetArray[ 1 ] = "quick";
   myTargetArray[ 2 ] = "brown";
   myTargetArray[ 3 ] = "fox";
   myTargetArray[ 4 ] = "jumped";
   myTargetArray[ 5 ] = "over";
   myTargetArray[ 6 ] = "the";
   myTargetArray[ 7 ] = "lazy";
   myTargetArray[ 8 ] = "dog";
   
   // Displays the values of the target Array.
   Console::WriteLine( "The target Array contains the following before:"
 );
   PrintValues( myTargetArray, ' ' );
   
   // Copies the keys in the source Hashtable to the target Hashtable,
 starting at index 6.
   Console::WriteLine( "After copying the keys, starting at index 6:" );
   mySourceHT->Keys->CopyTo( myTargetArray, 6 );
   
   // Displays the values of the target Array.
   PrintValues( myTargetArray, ' ' );
   
   // Copies the values in the source Hashtable to the target Hashtable,
 starting at index 6.
   Console::WriteLine( "After copying the values, starting at index 6:"
 );
   mySourceHT->Values->CopyTo( myTargetArray, 6 );
   
   // Displays the values of the target Array.
   PrintValues( myTargetArray, ' ' );
}

void PrintValues( array<String^>^myArr, char
 mySeparator )
{
   for ( int i = 0; i < myArr->Length;
 i++ )
      Console::Write( "{0}{1}", mySeparator, myArr[ i ] );
   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 The target Array contains the following before:
  The quick brown fox jumped over the lazy dog
 After copying the keys, starting at index 6:
  The quick brown fox jumped over B A dog
 After copying the values, starting at index 6:
  The quick brown fox jumped over valueB valueA dog

 */
import System.*;
import System.Collections.*;

public class SamplesHashtable
{
    public static void main(String[]
 args)
    {
        // Creates and initializes the source Hashtable.
        Hashtable mySourceHT = new Hashtable();

        mySourceHT.Add("A", "valueA");
        mySourceHT.Add("B", "valueB");

        // Creates and initializes the one-dimensional target Array.
        String myTargetArray[] = new String[15];

        myTargetArray.set_Item(0, "The");
        myTargetArray.set_Item(1, "quick");
        myTargetArray.set_Item(2, "brown");
        myTargetArray.set_Item(3, "fox");
        myTargetArray.set_Item(4, "jumped");
        myTargetArray.set_Item(5, "over");
        myTargetArray.set_Item(6, "the");
        myTargetArray.set_Item(7, "lazy");
        myTargetArray.set_Item(8, "dog");

        // Displays the values of the target Array.
        Console.WriteLine("The target Array contains the following before:");
        PrintValues(myTargetArray, ' ');

        // Copies the keys in the source Hashtable to the target Hashtable,
 
        // starting at index 6.
        Console.WriteLine("After copying the keys, starting at index 6:");
        mySourceHT.get_Keys().CopyTo(myTargetArray, 6);

        // Displays the values of the target Array.
        PrintValues(myTargetArray, ' ');

        // Copies the values in the source Hashtable to the target Hashtable,
 
        // starting at index 6.
        Console.WriteLine("After copying the values, starting at index 6:");
        mySourceHT.get_Values().CopyTo(myTargetArray, 6);

        // Displays the values of the target Array.
        PrintValues(myTargetArray, ' ');
    } //main

    public static void PrintValues(String
 myArr[], char mySeparator)
    {
        for (int i = 0; i < myArr.length;
 i++) {
            Console.Write("{0}{1}", System.Convert.ToString(mySeparator),
 
                myArr.get_Item(i));
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesHashtable
/* 
 This code produces the following output.
 
 The target Array contains the following before:
  The quick brown fox jumped over the lazy dog
 After copying the keys, starting at index 6:
  The quick brown fox jumped over B A dog
 After copying the values, starting at index 6:
  The quick brown fox jumped over valueB valueA dog
 */
import System
import System.Collections

// Creates and initializes the source Hashtable.
var mySourceHT : Hashtable = new Hashtable()
mySourceHT.Add("A", "valueA")
mySourceHT.Add("B", "valueB")

// Creates and initializes the one-dimensional target Array.
var myTargetArray : System.Array = System.Array.CreateInstance(System.String,
 15)
myTargetArray.SetValue("The", 0)
myTargetArray.SetValue("quick", 1)
myTargetArray.SetValue("brown", 2)
myTargetArray.SetValue("fox", 3)
myTargetArray.SetValue("jumped", 4)
myTargetArray.SetValue("over", 5)
myTargetArray.SetValue("the", 6)
myTargetArray.SetValue("lazy", 7)
myTargetArray.SetValue("dog", 8)

// Displays the values of the target Array.
Console.WriteLine("The target Array contains the following before:")
PrintValues(myTargetArray, " ")

// Copies the keys in the source Hashtable to the target Hashtable,
// starting at index 6.
Console.WriteLine("After copying the keys, starting at index 6:")
mySourceHT.Keys.CopyTo(myTargetArray, 6)

// Displays the values of the target Array.
PrintValues(myTargetArray, " ")

// Copies the values in the source Hashtable to the target Hashtable
,
// starting at index 6.
Console.WriteLine("After copying the values, starting at index 6:")
mySourceHT.Values.CopyTo(myTargetArray, 6)

// Displays the values of the target Array.
PrintValues(myTargetArray, " ")

function PrintValues(myArr : System.Array, mySeparator : Char){
    var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator()
    var i : int = 0
    var cols : int = myArr.GetLength(myArr.Rank
 - 1)
    while(myEnumerator.MoveNext()){
        if(i < cols)
            i++
        else{
            Console.WriteLine()
            i = 1
        }
        Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
    }
    Console.WriteLine()
}

// This code produces the following output.
// 
// The target Array contains the following before:
//  The quick brown fox jumped over the lazy dog
// After copying the keys, starting at index 6:
//  The quick brown fox jumped over A B dog
// After copying the values, starting at index 6:
//  The quick brown fox jumped over valueA valueB dog 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


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

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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2025 GRAS Group, Inc.RSS