ArrayList.CopyTo Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Copie ArrayList ou une partie de celui-ci dans un tableau unidimensionnel.
Surcharges
CopyTo(Array) |
Copie l’ensemble de ArrayList dans un Array compatible unidimensionnel en commençant au début du tableau cible. |
CopyTo(Array, Int32) |
Copie l'ensemble de l'objet ArrayList vers un objet Array unidimensionnel compatible, en commençant à l'index spécifié du tableau cible. |
CopyTo(Int32, Array, Int32, Int32) |
Copie une plage d’éléments de ArrayList vers un Array unidimensionnel compatible, en commençant à l’index spécifié du tableau cible. |
CopyTo(Array)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
public:
virtual void CopyTo(Array ^ array);
public virtual void CopyTo (Array array);
abstract member CopyTo : Array -> unit
override this.CopyTo : Array -> unit
Public Overridable Sub CopyTo (array As Array)
Paramètres
- array
- Array
Array unidimensionnel qui constitue la destination des éléments copiés à partir de ArrayList. Array doit avoir une indexation de base zéro.
Exceptions
array
a la valeur null
.
array
est multidimensionnel.
- ou -
Le nombre d’éléments du ArrayList source est supérieur au nombre d’éléments que la destination array
peut contenir.
Le type du ArrayList source ne peut pas être converti automatiquement en type du array
de destination.
Exemples
L’exemple de code suivant montre comment copier un dans ArrayList un System.Array.
using namespace System;
using namespace System::Collections;
void PrintValues( array<String^>^myArr, char mySeparator );
int main()
{
// Creates and initializes the source ArrayList.
ArrayList^ mySourceList = gcnew ArrayList;
mySourceList->Add( "three" );
mySourceList->Add( "napping" );
mySourceList->Add( "cats" );
mySourceList->Add( "in" );
mySourceList->Add( "the" );
mySourceList->Add( "barn" );
// 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 and after copying):" );
PrintValues( myTargetArray, ' ' );
// Copies the second element from the source ArrayList to the target ArrayList starting at index 7.
mySourceList->CopyTo( 1, myTargetArray, 7, 1 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target ArrayList starting at index 6.
mySourceList->CopyTo( myTargetArray, 6 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target ArrayList starting at index 0.
mySourceList->CopyTo( myTargetArray );
// 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 and after copying):
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the napping dog
The quick brown fox jumps over three napping cats in the barn
three napping cats in the barn three napping cats in the barn
*/
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes the source ArrayList.
ArrayList mySourceList = new ArrayList();
mySourceList.Add( "three" );
mySourceList.Add( "napping" );
mySourceList.Add( "cats" );
mySourceList.Add( "in" );
mySourceList.Add( "the" );
mySourceList.Add( "barn" );
// 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 and after copying):" );
PrintValues( myTargetArray, ' ' );
// Copies the second element from the source ArrayList to the target Array starting at index 7.
mySourceList.CopyTo( 1, myTargetArray, 7, 1 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array starting at index 6.
mySourceList.CopyTo( myTargetArray, 6 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array starting at index 0.
mySourceList.CopyTo( myTargetArray );
// 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 and after copying):
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the napping dog
The quick brown fox jumps over three napping cats in the barn
three napping cats in the barn three napping cats in the barn
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes the source ArrayList.
Dim mySourceList As New ArrayList()
mySourceList.Add("three")
mySourceList.Add("napping")
mySourceList.Add("cats")
mySourceList.Add("in")
mySourceList.Add("the")
mySourceList.Add("barn")
' 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 and after copying):")
PrintValues(myTargetArray, " "c)
' Copies the second element from the source ArrayList to the target
' Array starting at index 7.
mySourceList.CopyTo(1, myTargetArray, 7, 1)
' Displays the values of the target Array.
PrintValues(myTargetArray, " "c)
' Copies the entire source ArrayList to the target Array starting
' at index 6.
mySourceList.CopyTo(myTargetArray, 6)
' Displays the values of the target Array.
PrintValues(myTargetArray, " "c)
' Copies the entire source ArrayList to the target Array starting
' at index 0.
mySourceList.CopyTo(myTargetArray)
' Displays the values of the target Array.
PrintValues(myTargetArray, " "c)
End Sub
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
End Class
' This code produces the following output.
'
' The target Array contains the following (before and after copying):
' The quick brown fox jumps over the lazy dog
' The quick brown fox jumps over the napping dog
' The quick brown fox jumps over three napping cats in the barn
' three napping cats in the barn three napping cats in the barn
Remarques
Le tableau spécifié doit être d’un type compatible.
Cette méthode utilise Array.Copy pour copier les éléments.
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 ArrayList.
Cette méthode est une O(n)
opération, où n
est Count.
S’applique à
CopyTo(Array, Int32)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
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 éléments copiés à partir de ArrayList. 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 ArrayList source est supérieur à la quantité d’espace disponible entre arrayIndex
et la fin du array
de destination.
Le type du ArrayList source ne peut pas être converti automatiquement en type du array
de destination.
Exemples
L’exemple de code suivant montre comment copier un dans ArrayList un System.Array.
using namespace System;
using namespace System::Collections;
void PrintValues( array<String^>^myArr, char mySeparator );
int main()
{
// Creates and initializes the source ArrayList.
ArrayList^ mySourceList = gcnew ArrayList;
mySourceList->Add( "three" );
mySourceList->Add( "napping" );
mySourceList->Add( "cats" );
mySourceList->Add( "in" );
mySourceList->Add( "the" );
mySourceList->Add( "barn" );
// 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 and after copying):" );
PrintValues( myTargetArray, ' ' );
// Copies the second element from the source ArrayList to the target Array, starting at index 7.
mySourceList->CopyTo( 1, myTargetArray, 7, 1 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 6.
mySourceList->CopyTo( myTargetArray, 6 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 0.
mySourceList->CopyTo( myTargetArray );
// 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 and after copying):
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the napping dog
The quick brown fox jumps over three napping cats in the barn
three napping cats in the barn three napping cats in the barn
*/
using System;
using System.Collections;
public class SamplesArrayList1 {
public static void Main() {
// Creates and initializes the source ArrayList.
ArrayList mySourceList = new ArrayList();
mySourceList.Add( "three" );
mySourceList.Add( "napping" );
mySourceList.Add( "cats" );
mySourceList.Add( "in" );
mySourceList.Add( "the" );
mySourceList.Add( "barn" );
// 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 and after copying):" );
PrintValues( myTargetArray, ' ' );
// Copies the second element from the source ArrayList to the target Array, starting at index 7.
mySourceList.CopyTo( 1, myTargetArray, 7, 1 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 6.
mySourceList.CopyTo( myTargetArray, 6 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 0.
mySourceList.CopyTo( myTargetArray );
// 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 and after copying):
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the napping dog
The quick brown fox jumps over three napping cats in the barn
three napping cats in the barn three napping cats in the barn
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes the source ArrayList.
Dim mySourceList As New ArrayList()
mySourceList.Add("three")
mySourceList.Add("napping")
mySourceList.Add("cats")
mySourceList.Add("in")
mySourceList.Add("the")
mySourceList.Add("barn")
' 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 and after copying):")
PrintValues(myTargetArray, " "c)
' Copies the second element from the source ArrayList to the target Array, starting at index 7.
mySourceList.CopyTo(1, myTargetArray, 7, 1)
' Displays the values of the target Array.
PrintValues(myTargetArray, " "c)
' Copies the entire source ArrayList to the target Array, starting at index 6.
mySourceList.CopyTo(myTargetArray, 6)
' Displays the values of the target Array.
PrintValues(myTargetArray, " "c)
' Copies the entire source ArrayList to the target Array, starting at index 0.
mySourceList.CopyTo(myTargetArray)
' Displays the values of the target Array.
PrintValues(myTargetArray, " "c)
End Sub
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
End Class
' This code produces the following output.
'
' The target Array contains the following (before and after copying):
' The quick brown fox jumps over the lazy dog
' The quick brown fox jumps over the napping dog
' The quick brown fox jumps over three napping cats in the barn
' three napping cats in the barn three napping cats in the barn
Remarques
Le tableau spécifié doit être d’un type compatible.
Cette méthode utilise Array.Copy pour copier les éléments.
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 ArrayList.
Cette méthode est une O(n)
opération, où n
est Count.
S’applique à
CopyTo(Int32, Array, Int32, Int32)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
public:
virtual void CopyTo(int index, Array ^ array, int arrayIndex, int count);
public virtual void CopyTo (int index, Array array, int arrayIndex, int count);
abstract member CopyTo : int * Array * int * int -> unit
override this.CopyTo : int * Array * int * int -> unit
Public Overridable Sub CopyTo (index As Integer, array As Array, arrayIndex As Integer, count As Integer)
Paramètres
- array
- Array
Array unidimensionnel qui constitue la destination des éléments copiés à partir de ArrayList. Array doit avoir une indexation de base zéro.
- arrayIndex
- Int32
Index de base zéro dans l'array
à partir duquel la copie commence.
- count
- Int32
Nombre d’éléments à copier.
Exceptions
array
a la valeur null
.
index
est inférieur à zéro.
- ou -
arrayIndex
est inférieur à zéro.
- ou -
count
est inférieur à zéro.
array
est multidimensionnel.
- ou -
index
est égal ou supérieur au Count de la source ArrayList.
- ou -
Le nombre d’éléments entre index
et la fin de la source ArrayList est supérieur à l’espace disponible entre arrayIndex
et la fin de la destination array
.
Le type du ArrayList source ne peut pas être converti automatiquement en type du array
de destination.
Exemples
L’exemple de code suivant montre comment copier un dans ArrayList un System.Array.
using namespace System;
using namespace System::Collections;
void PrintValues( array<String^>^myArr, char mySeparator );
int main()
{
// Creates and initializes the source ArrayList.
ArrayList^ mySourceList = gcnew ArrayList;
mySourceList->Add( "three" );
mySourceList->Add( "napping" );
mySourceList->Add( "cats" );
mySourceList->Add( "in" );
mySourceList->Add( "the" );
mySourceList->Add( "barn" );
// 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 and after copying):" );
PrintValues( myTargetArray, ' ' );
// Copies the second element from the source ArrayList to the target Array, starting at index 7.
mySourceList->CopyTo( 1, myTargetArray, 7, 1 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 6.
mySourceList->CopyTo( myTargetArray, 6 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 0.
mySourceList->CopyTo( myTargetArray );
// 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 and after copying):
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the napping dog
The quick brown fox jumps over three napping cats in the barn
three napping cats in the barn three napping cats in the barn
*/
using System;
using System.Collections;
public class SamplesArrayList1 {
public static void Main() {
// Creates and initializes the source ArrayList.
ArrayList mySourceList = new ArrayList();
mySourceList.Add( "three" );
mySourceList.Add( "napping" );
mySourceList.Add( "cats" );
mySourceList.Add( "in" );
mySourceList.Add( "the" );
mySourceList.Add( "barn" );
// 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 and after copying):" );
PrintValues( myTargetArray, ' ' );
// Copies the second element from the source ArrayList to the target Array, starting at index 7.
mySourceList.CopyTo( 1, myTargetArray, 7, 1 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 6.
mySourceList.CopyTo( myTargetArray, 6 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 0.
mySourceList.CopyTo( myTargetArray );
// 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 and after copying):
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the napping dog
The quick brown fox jumps over three napping cats in the barn
three napping cats in the barn three napping cats in the barn
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes the source ArrayList.
Dim mySourceList As New ArrayList()
mySourceList.Add("three")
mySourceList.Add("napping")
mySourceList.Add("cats")
mySourceList.Add("in")
mySourceList.Add("the")
mySourceList.Add("barn")
' 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 and after copying):")
PrintValues(myTargetArray, " "c)
' Copies the second element from the source ArrayList to the target Array, starting at index 7.
mySourceList.CopyTo(1, myTargetArray, 7, 1)
' Displays the values of the target Array.
PrintValues(myTargetArray, " "c)
' Copies the entire source ArrayList to the target Array, starting at index 6.
mySourceList.CopyTo(myTargetArray, 6)
' Displays the values of the target Array.
PrintValues(myTargetArray, " "c)
' Copies the entire source ArrayList to the target Array, starting at index 0.
mySourceList.CopyTo(myTargetArray)
' Displays the values of the target Array.
PrintValues(myTargetArray, " "c)
End Sub
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
End Class
' This code produces the following output.
'
' The target Array contains the following (before and after copying):
' The quick brown fox jumps over the lazy dog
' The quick brown fox jumps over the napping dog
' The quick brown fox jumps over three napping cats in the barn
' three napping cats in the barn three napping cats in the barn
Remarques
Le tableau spécifié doit être d’un type compatible.
Cette méthode utilise Array.Copy pour copier les éléments.
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 ArrayList.
Cette méthode est une O(n)
opération, où n
est count
.