Hashtable.CopyTo(Array, Int32) Méthode

Définition

Copie les éléments Hashtable dans une instance de Array unidimensionnelle à l'index spécifié.

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
Public Overridable Sub CopyTo (array As Array, arrayIndex As Integer)

Paramètres

array
Array

Array unidimensionnel qui constitue la destination des objets DictionaryEntry copiés à partir de Hashtable. Array doit avoir une indexation de base zéro.

arrayIndex
Int32

Index de base zéro dans l'array à partir duquel la copie commence.

Implémente

Exceptions

array a la valeur null.

arrayIndex est inférieur à zéro.

array est multidimensionnel.

- ou -

Le nombre d’éléments dans le Hashtable source est supérieur à la quantité d’espace disponible entre arrayIndex et la fin du array de destination.

Le type du Hashtable source ne peut pas être converti automatiquement en type du array de destination.

Exemples

L’exemple suivant montre comment copier la liste de clés ou la liste de valeurs d’un Hashtable dans un unidimensionnel 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.
      var mySourceHT = new Hashtable();
      mySourceHT.Add("A", "valueA");
      mySourceHT.Add("B", "valueB");

      // Creates and initializes the one-dimensional target Array.
      var 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($"{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.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

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

End Class


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

Remarques

Les éléments sont copiés dans le Array dans le même ordre que celui dans lequel l’énumérateur effectue une itération dans le Hashtable.

Pour copier uniquement les clés dans , Hashtableutilisez Hashtable.Keys.CopyTo.

Pour copier uniquement les valeurs dans , Hashtableutilisez Hashtable.Values.CopyTo.

Cette méthode est une O(n) opération, où n est Count.

S’applique à

Voir aussi