ArrayList.CopyTo 方法

定义

ArrayList 或它的一部分复制到一维数组。

重载

CopyTo(Array)

从目标数组的开头开始,将整个 ArrayList 复制到兼容的一维 Array

CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 ArrayList 复制到兼容的一维 Array

CopyTo(Int32, Array, Int32, Int32)

从目标数组的指定索引处开始,将 ArrayList 中某个范围的元素复制到兼容的一维数组 Array

CopyTo(Array)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

从目标数组的开头开始,将整个 ArrayList 复制到兼容的一维 Array

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)

参数

array
Array

一维 Array,它是从 ArrayList 复制的元素的目标。 Array 必须具有从零开始的索引。

例外

arraynull

array 是多维的。

- 或 -

ArrayList 中的元素个数大于目标 array 可以包含的元素个数。

无法自动将源 ArrayList 的类型转换为目标 array 的类型。

示例

下面的代码示例演示如何将 复制到 ArrayList 一维 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

注解

指定的数组必须属于兼容类型。

此方法使用 Array.Copy 复制元素。

元素将按照枚举器循环访问 的相同顺序复制到 ArrayArrayList

此方法是一个 O(n) 操作,其中 nCount

适用于

CopyTo(Array, Int32)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

从目标数组的指定索引处开始将整个 ArrayList 复制到兼容的一维 Array

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)

参数

array
Array

一维 Array,它是从 ArrayList 复制的元素的目标。 Array 必须具有从零开始的索引。

arrayIndex
Int32

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

实现

例外

arraynull

arrayIndex 小于零。

array 是多维的。

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

无法自动将源 ArrayList 的类型转换为目标 array 的类型。

示例

下面的代码示例演示如何将 复制到 ArrayList 一维 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

注解

指定的数组必须属于兼容类型。

此方法使用 Array.Copy 复制元素。

元素将按照枚举器循环访问 的相同顺序复制到 ArrayArrayList

此方法是一个 O(n) 操作,其中 nCount

适用于

CopyTo(Int32, Array, Int32, Int32)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

从目标数组的指定索引处开始,将 ArrayList 中某个范围的元素复制到兼容的一维数组 Array

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)

参数

index
Int32

复制即从源 ArrayList 中从零开始的索引开始。

array
Array

一维 Array,它是从 ArrayList 复制的元素的目标。 Array 必须具有从零开始的索引。

arrayIndex
Int32

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

count
Int32

要复制的元素数。

例外

arraynull

index 小于零。

arrayIndex 小于零。

count 小于零。

array 是多维的。

- 或 -

index 等于或大于源 ArrayListCount

- 或 -

index 到源 ArrayList 的末尾的元素数大于从 arrayIndex 到目标 array 的末尾的可用空间。

无法自动将源 ArrayList 的类型转换为目标 array 的类型。

示例

下面的代码示例演示如何将 复制到 ArrayList 一维 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

注解

指定的数组必须属于兼容类型。

此方法使用 Array.Copy 复制元素。

元素将按照枚举器循环访问 的相同顺序复制到 ArrayArrayList

此方法是一个 O(n) 操作,其中 ncount

适用于