ArrayList.CopyTo Metodo

Definizione

Copia ArrayList o una parte di esso in una matrice unidimensionale.

Overload

CopyTo(Array)

Copia l'intero oggetto ArrayList in un oggetto Array unidimensionale compatibile, a partire dall'inizio della matrice di destinazione.

CopyTo(Array, Int32)

Copia l'intero oggetto ArrayList in un oggetto Array compatibile unidimensionale, a partire dall'indice specificato della matrice di destinazione.

CopyTo(Int32, Array, Int32, Int32)

Copia una serie di elementi da ArrayList in un oggetto Array compatibile unidimensionale, a partire dall'indice specificato della matrice di destinazione.

CopyTo(Array)

Origine:
ArrayList.cs
Origine:
ArrayList.cs
Origine:
ArrayList.cs

Copia l'intero oggetto ArrayList in un oggetto Array unidimensionale compatibile, a partire dall'inizio della matrice di destinazione.

C#
public virtual void CopyTo (Array array);

Parametri

array
Array

Oggetto Array unidimensionale che rappresenta la destinazione degli elementi copiati dall'oggetto ArrayList. L'indicizzazione di Array deve essere in base zero.

Eccezioni

array è null.

array è multidimensionale.

-oppure-

Il numero di elementi nell'oggetto ArrayList di origine è maggiore del numero di elementi che l'oggetto array di destinazione può contenere.

Non è possibile eseguire il cast automatico del tipo dell'oggetto ArrayList di origine al tipo dell'oggetto array di destinazione.

Esempio

Nell'esempio di codice seguente viene illustrato come copiare un oggetto ArrayList in un oggetto unidimensionale System.Array.

C#
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
*/

Commenti

La matrice specificata deve essere di un tipo compatibile.

Questo metodo usa per copiare Array.Copy gli elementi.

Gli elementi vengono copiati Array nello stesso ordine in cui l'enumeratore esegue l'iterazione tramite ArrayList.

Questo metodo è un'operazione O(n) , dove n è Count.

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

CopyTo(Array, Int32)

Origine:
ArrayList.cs
Origine:
ArrayList.cs
Origine:
ArrayList.cs

Copia l'intero oggetto ArrayList in un oggetto Array compatibile unidimensionale, a partire dall'indice specificato della matrice di destinazione.

C#
public virtual void CopyTo (Array array, int arrayIndex);

Parametri

array
Array

Oggetto Array unidimensionale che rappresenta la destinazione degli elementi copiati dall'oggetto ArrayList. L'indicizzazione di Array deve essere in base zero.

arrayIndex
Int32

Indice in base zero in array in corrispondenza del quale viene avviata la copia.

Implementazioni

Eccezioni

array è null.

arrayIndex è minore di zero.

array è multidimensionale.

-oppure-

Il numero di elementi nell'oggetto ArrayList di origine è maggiore dello spazio disponibile tra arrayIndex e la fine dell'oggetto array di destinazione.

Non è possibile eseguire il cast automatico del tipo dell'oggetto ArrayList di origine al tipo dell'oggetto array di destinazione.

Esempio

Nell'esempio di codice seguente viene illustrato come copiare un oggetto ArrayList in un oggetto unidimensionale System.Array.

C#
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

*/

Commenti

La matrice specificata deve essere di un tipo compatibile.

Questo metodo usa per copiare Array.Copy gli elementi.

Gli elementi vengono copiati Array nello stesso ordine in cui l'enumeratore esegue l'iterazione tramite ArrayList.

Questo metodo è un'operazione O(n) , dove n è Count.

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

CopyTo(Int32, Array, Int32, Int32)

Origine:
ArrayList.cs
Origine:
ArrayList.cs
Origine:
ArrayList.cs

Copia una serie di elementi da ArrayList in un oggetto Array compatibile unidimensionale, a partire dall'indice specificato della matrice di destinazione.

C#
public virtual void CopyTo (int index, Array array, int arrayIndex, int count);

Parametri

index
Int32

Indice in base zero dell'oggetto ArrayList di origine a partire dal quale viene effettuata la copia.

array
Array

Oggetto Array unidimensionale che rappresenta la destinazione degli elementi copiati dall'oggetto ArrayList. L'indicizzazione di Array deve essere in base zero.

arrayIndex
Int32

Indice in base zero in array in corrispondenza del quale viene avviata la copia.

count
Int32

Numero degli elementi da copiare.

Eccezioni

array è null.

index è minore di zero.

-oppure-

arrayIndex è minore di zero.

-oppure-

count è minore di zero.

array è multidimensionale.

-oppure-

index è maggiore o uguale al valore di Count dell'oggetto ArrayList di origine.

-oppure-

Il numero di elementi da index alla fine dell'oggetto ArrayList di origine è maggiore dello spazio disponibile da arrayIndex alla fine dell'oggetto array di destinazione.

Non è possibile eseguire il cast automatico del tipo dell'oggetto ArrayList di origine al tipo dell'oggetto array di destinazione.

Esempio

Nell'esempio di codice seguente viene illustrato come copiare un oggetto ArrayList in un oggetto unidimensionale System.Array.

C#
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

*/

Commenti

La matrice specificata deve essere di un tipo compatibile.

Questo metodo usa per copiare Array.Copy gli elementi.

Gli elementi vengono copiati Array nello stesso ordine in cui l'enumeratore esegue l'iterazione tramite ArrayList.

Questo metodo è un'operazione O(n) , dove n è count.

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0