Hashtable.CopyTo(Array, Int32) Hashtable.CopyTo(Array, Int32) Hashtable.CopyTo(Array, Int32) Method

정의

Hashtable 요소를 지정한 인덱스의 1차원 Array 인스턴스에 복사합니다.Copies the Hashtable elements to a one-dimensional Array instance at the specified index.

public:
 virtual void CopyTo(Array ^ array, int arrayIndex);
public virtual void CopyTo (Array array, int arrayIndex);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit

매개 변수

array
Array Array Array

Array에서 복사한 DictionaryEntry 개체의 대상인 1차원 Hashtable 배열입니다.The one-dimensional Array that is the destination of the DictionaryEntry objects copied from Hashtable. Array에는 0부터 시작하는 인덱스가 있어야 합니다.The Array must have zero-based indexing.

arrayIndex
Int32 Int32 Int32

array에서 복사가 시작되는 0부터 시작하는 인덱스입니다.The zero-based index in array at which copying begins.

구현

예외

arraynull입니다.array is null.

arrayIndex가 0보다 작습니다.arrayIndex is less than zero.

array가 다차원 배열인 경우array is multidimensional.

또는-or- 소스 Hashtable의 요소 수가 대상 arrayarrayIndex부터 끝까지 사용 가능한 공간보다 큽니다.The number of elements in the source Hashtable is greater than the available space from arrayIndex to the end of the destination array.

소스 Hashtable의 형식을 대상 array의 형식으로 자동 캐스팅할 수 없습니다.The type of the source Hashtable cannot be cast automatically to the type of the destination array.

예제

다음 예제에서는 키 목록을 가져오거나의 값 목록을 복사 하는 방법을 보여 줍니다는 Hashtable 는 1 차원으로 Array입니다.The following example shows how to copy the list of keys or the list of values in a Hashtable into a one-dimensional Array.

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 ] = "jumps";
   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 jumps over the lazy dog
 After copying the keys, starting at index 6:
  The quick brown fox jumps over B A dog
 After copying the values, starting at index 6:
  The quick brown fox jumps 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] = "jumps";
      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 jumps over the lazy dog
After copying the keys, starting at index 6:
 The quick brown fox jumps over B A dog
After copying the values, starting at index 6:
 The quick brown fox jumps over valueB valueA dog

*/ 
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) = "jumps"
        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 jumps over the lazy dog
' After copying the keys, starting at index 6:
'  The quick brown fox jumps over B A dog
' After copying the values, starting at index 6:
'  The quick brown fox jumps over valueB valueA dog

설명

요소에 복사 됩니다는 Array 열거자가 반복 나타나는 순서에서는 Hashtable합니다.The elements are copied to the Array in the same order in which the enumerator iterates through the Hashtable.

에 있는 키를 복사 하는 Hashtable를 사용 하 여 Hashtable.Keys.CopyTo입니다.To copy only the keys in the Hashtable, use Hashtable.Keys.CopyTo.

값만 복사 하는 Hashtable를 사용 하 여 Hashtable.Values.CopyTo입니다.To copy only the values in the Hashtable, use Hashtable.Values.CopyTo.

이 메서드는 O (n) 작업, 여기서 nCount합니다.This method is an O(n) operation, where n is Count.

적용 대상

추가 정보