ArrayList.CopyTo 方法

定義

ArrayList 或其中一部分複製至一維陣列。

多載

CopyTo(Array)

從目標陣列的開頭開始,將整個 ArrayList 複製至相容的一維 Array

CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 ArrayList 複製到相容的一維 Array

CopyTo(Int32, Array, Int32, Int32)

從目標陣列的指定索引開始,將項目範圍從 ArrayList 複製至相容的一維 Array

CopyTo(Array)

來源:
ArrayList.cs
來源:
ArrayList.cs
來源:
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 來複製專案。

元素會以逐一查看 ArrayList的相同順序複製到 Array

這個方法是作業 O(n) ,其中 nCount

適用於

CopyTo(Array, Int32)

來源:
ArrayList.cs
來源:
ArrayList.cs
來源:
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 來複製專案。

元素會以逐一查看 ArrayList的相同順序複製到 Array

這個方法是作業 O(n) ,其中 nCount

適用於

CopyTo(Int32, Array, Int32, Int32)

來源:
ArrayList.cs
來源:
ArrayList.cs
來源:
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 來複製專案。

元素會以逐一查看 ArrayList的相同順序複製到 Array

這個方法是作業 O(n) ,其中 ncount

適用於