Hashtable.CopyTo 方法

Hashtable 元素复制到一维 Array 实例中的指定索引位置。

**命名空间:**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 virtual void CopyTo (
    Array array,
    int arrayIndex
)
public:
virtual void CopyTo (
    Array^ array, 
    int arrayIndex
)
public void CopyTo (
    Array array, 
    int arrayIndex
)
public function CopyTo (
    array : Array, 
    arrayIndex : int
)

参数

  • arrayIndex
    array 中的从零开始的索引,从此处开始复制。

异常

异常类型 条件

ArgumentNullException

array 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentOutOfRangeException

arrayIndex 小于零。

ArgumentException

array 是多维的。

- 或 -

arrayIndex 等于或大于 array 的长度。

- 或 -

Hashtable 中的元素数目大于从 arrayIndex 到目标 array 末尾之间的可用空间。

InvalidCastException

Hashtable 的类型无法自动转换为目标 array 的类型。

备注

元素按照枚举数循环访问 Hashtable 的顺序复制到 Array 中。

若要只复制 Hashtable 中的键,请使用 Hashtable.Keys.CopyTo

若要只复制 Hashtable 中的值,请使用 Hashtable.Values.CopyTo

此方法的运算复杂度为 O(n),其中 n 是 Count

示例

下面的示例说明如何将 Hashtable 中键的列表或值的列表复制到一维 Array 中。

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 

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Hashtable 类
Hashtable 成员
System.Collections 命名空间
Array 类
DictionaryEntry 结构
GetEnumerator