Hashtable.CopyTo-Methode
Kopiert die Hashtable-Elemente an den angegebenen Index in einer eindimensionalen Array-Instanz.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Overridable Sub CopyTo ( _
array As Array, _
arrayIndex As Integer _
)
'Usage
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
)
Parameter
- array
Das eindimensionale Array, das das Ziel der aus Hashtable kopierten DictionaryEntry-Objekte ist. Für Array muss eine nullbasierte Indizierung verwendet werden.
- arrayIndex
Der nullbasierte Index in array, ab dem kopiert wird.
Ausnahmen
Ausnahmetyp | Bedingung |
---|---|
array ist NULL (Nothing in Visual Basic). |
|
arrayIndex ist kleiner als null. |
|
array ist mehrdimensional. – oder – Der arrayIndex ist größer oder gleich der Länge des array. – oder – Die Anzahl der aus der Quell-Hashtable zu kopierenden Elemente ist größer als der verfügbare Platz von arrayIndex bis zum Ende des Ziel-array. |
|
Der Typ der Quell-Hashtable kann nicht automatisch in den Typ des Ziel-array umgewandelt werden. |
Hinweise
Die Elemente werden in derselben Reihenfolge nach Array kopiert, in der der Enumerator die Hashtable durchläuft.
Verwenden Sie Hashtable.Keys.CopyTo
, wenn Sie lediglich die Schlüssel in der Hashtable kopieren möchten.
Verwenden Sie Hashtable.Values.CopyTo
, wenn Sie lediglich die Werte in der Hashtable kopieren möchten.
Diese Methode ist eine O(n)-Operation, wobei n gleich Count ist.
Beispiel
Im folgenden Beispiel wird gezeigt, wie die Schlüsselliste oder die Werteliste aus einer Hashtable in ein eindimensionales Array kopiert wird.
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
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
Hashtable-Klasse
Hashtable-Member
System.Collections-Namespace
Array-Klasse
DictionaryEntry-Struktur
GetEnumerator