共用方式為


Array.CopyTo 方法

定義

將目前一維陣列的所有項目複製到指定的一維陣列。

多載

CopyTo(Array, Int32)

將目前一維陣列的所有項目複製到指定的一維陣列 (從指定的目的陣列索引開始)。 索引已指定為 32 位元整數。

CopyTo(Array, Int64)

將目前一維陣列的所有項目複製到指定的一維陣列 (從指定的目的陣列索引開始)。 索引已指定為 64 位元整數。

範例

下列程式碼範例示範如何將 複製到 Array 另一個 Array

using namespace System;

void main()
{
   // Creates and initializes two new Array instances.
   Array^ mySourceArray = Array::CreateInstance(String::typeid, 6);
   mySourceArray->SetValue("three", 0);
   mySourceArray->SetValue("napping", 1);
   mySourceArray->SetValue("cats", 2);
   mySourceArray->SetValue("in", 3);
   mySourceArray->SetValue("the", 4);
   mySourceArray->SetValue("barn", 5);
   Array^ myTargetArray = Array::CreateInstance(String::typeid, 15);
   myTargetArray->SetValue("The", 0);
   myTargetArray->SetValue("quick", 1);
   myTargetArray->SetValue("brown", 2);
   myTargetArray->SetValue("fox", 3);
   myTargetArray->SetValue("jumps", 4);
   myTargetArray->SetValue("over", 5);
   myTargetArray->SetValue("the", 6);
   myTargetArray->SetValue("lazy", 7);
   myTargetArray->SetValue("dog", 8);

   // Displays the values of the Array.
   Console::WriteLine( "The target Array instance contains the following (before and after copying):");
   PrintValues(myTargetArray);

   // Copies the source Array to the target Array, starting at index 6.
   mySourceArray->CopyTo(myTargetArray, 6);

   // Displays the values of the Array.
   PrintValues(myTargetArray);
}

void PrintValues(Array^ myArr)
{
   System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator();
   int i = 0;
   int cols = myArr->GetLength(myArr->Rank - 1);
   while (myEnumerator->MoveNext())
   {
      if (i < cols)
      {
         i++;
      }
      else
      {
         Console::WriteLine();
         i = 1;
      }

      Console::Write( " {0}", myEnumerator->Current);
   }

   Console::WriteLine();
}

/*
 This code produces the following output.
 
  The target Array instance contains the following (before and after copying):
  The quick brown fox jumps over the lazy dog      
  The quick brown fox jumps over three napping cats in the barn
 */
let printValues arr sep =
    for i in arr do
        printf $"{sep}{i}"
    printfn ""

// Creates and initializes two new Arrays.
let mySourceArray = 
    [| "three"
       "napping"
       "cats"
       "in"
       "the"
       "barn" |]

let myTargetArray = Array.zeroCreate 15
myTargetArray[0..8] <-
    [| "The"
       "quick"
       "brown"
       "fox"
       "jumps"
       "over"
       "the"
       "lazy"
       "dog" |]

// Displays the values of the Array.
printfn "The target Array contains the following (before and after copying):"
printValues myTargetArray ' '

// Copies the source Array to the target Array, starting at index 6.
mySourceArray.CopyTo(myTargetArray, 6)

// Displays the values of the Array.
printValues myTargetArray ' '


// 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 three napping cats in the barn
using System;

public class SamplesArray
{

   public static void Main()
   {

      // Creates and initializes two new Arrays.
      Array mySourceArray=Array.CreateInstance(typeof(string), 6);
      mySourceArray.SetValue("three", 0);
      mySourceArray.SetValue("napping", 1);
      mySourceArray.SetValue("cats", 2);
      mySourceArray.SetValue("in", 3);
      mySourceArray.SetValue("the", 4);
      mySourceArray.SetValue("barn", 5);
      Array myTargetArray=Array.CreateInstance(typeof(string), 15);
      myTargetArray.SetValue("The", 0);
      myTargetArray.SetValue("quick", 1);
      myTargetArray.SetValue("brown", 2);
      myTargetArray.SetValue("fox", 3);
      myTargetArray.SetValue("jumps", 4);
      myTargetArray.SetValue("over", 5);
      myTargetArray.SetValue("the", 6);
      myTargetArray.SetValue("lazy", 7);
      myTargetArray.SetValue("dog", 8);

      // Displays the values of the Array.
      Console.WriteLine("The target Array contains the following (before and after copying):");
      PrintValues(myTargetArray, ' ');

      // Copies the source Array to the target Array, starting at index 6.
      mySourceArray.CopyTo(myTargetArray, 6);

      // Displays the values of the Array.
      PrintValues(myTargetArray, ' ');
   }

   public static void PrintValues(Array myArr, char mySeparator)
   {

      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = 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 and after copying):
 The quick brown fox jumps over the lazy dog
 The quick brown fox jumps over three napping cats in the barn
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes two new Arrays.
        Dim mySourceArray As Array = Array.CreateInstance(GetType(String), 6)
        mySourceArray.SetValue("three", 0)
        mySourceArray.SetValue("napping", 1)
        mySourceArray.SetValue("cats", 2)
        mySourceArray.SetValue("in", 3)
        mySourceArray.SetValue("the", 4)
        mySourceArray.SetValue("barn", 5)
        Dim myTargetArray As Array = Array.CreateInstance(GetType(String), 15)
        myTargetArray.SetValue("The", 0)
        myTargetArray.SetValue("quick", 1)
        myTargetArray.SetValue("brown", 2)
        myTargetArray.SetValue("fox", 3)
        myTargetArray.SetValue("jumps", 4)
        myTargetArray.SetValue("over", 5)
        myTargetArray.SetValue("the", 6)
        myTargetArray.SetValue("lazy", 7)
        myTargetArray.SetValue("dog", 8)
        
        ' Displays the values of the Array.
        Console.WriteLine("The target Array contains the following" _
           & "(before and after copying):")
        PrintValues(myTargetArray, " "c)
        
        ' Copies the source Array to the target Array, starting at index 6.
        mySourceArray.CopyTo(myTargetArray, 6)
        
        ' Displays the values of the Array.
        PrintValues(myTargetArray, " "c)
    End Sub    
    
    Public Shared Sub PrintValues(myArr As Array, mySeparator As Char)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        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 three napping cats in the barn

下列程式碼範例示範如何使用非零下限將 複製到 Array 另一個 Array 。 請注意,會複製整個來源 Array ,包括覆寫目標 Array 中現有專案的空白專案。

using namespace System;

void main()
{
   // Creates and initializes the source Array.
   Array^ myArrayZero = Array::CreateInstance(String::typeid, 3);
   myArrayZero->SetValue("zero", 0);
   myArrayZero->SetValue("one", 1);

   // Displays the source Array.
   Console::WriteLine("The array with lowbound=0 contains:");
   PrintIndexAndValues(myArrayZero);

   // Creates and initializes the target Array.
   array<int>^myArrLen = {4};
   array<int>^myArrLow = {2};
   Array^ myArrayTwo = Array::CreateInstance(String::typeid, myArrLen, myArrLow);
   myArrayTwo->SetValue("two", 2);
   myArrayTwo->SetValue("three", 3);
   myArrayTwo->SetValue("four", 4);
   myArrayTwo->SetValue("five", 5);

   // Displays the target Array.
   Console::WriteLine("The array with lowbound=2 contains:");
   PrintIndexAndValues(myArrayTwo);

   // Copy from the array with lowbound=0 to the array with lowbound=2.
   myArrayZero->CopyTo(myArrayTwo, 3);

   // Displays the modified target Array.
   Console::WriteLine("\nAfter copying at relative index 1:");
   PrintIndexAndValues(myArrayTwo);
}

void PrintIndexAndValues(Array^ myArray)
{
   for (int i = myArray->GetLowerBound(0); i <= myArray->GetUpperBound(0); i++)
      Console::WriteLine("\t[{0}]:\t{1}", i, myArray->GetValue(i));
}

/* 
 This code produces the following output.
 
 The array with lowbound=0 contains:
     [0]:    zero
     [1]:    one
     [2]:    
 The array with lowbound=2 contains:
     [2]:    two
     [3]:    three
     [4]:    four
     [5]:    five
 
 After copying at relative index 1:
     [2]:    two
     [3]:    zero
     [4]:    one
     [5]:
 */
open System

let printIndexAndValues (myArray: Array) =
    for i = myArray.GetLowerBound 0 to myArray.GetUpperBound 0 do
        printfn $"\t[{i}]:\t{myArray.GetValue i}"

// Creates and initializes the source Array.
let myArrayZero = Array.zeroCreate 3
myArrayZero[0] <- "zero"
myArrayZero[1] <- "one"

// Displays the source Array.
printfn "The array with lower bound=0 contains:"
printIndexAndValues myArrayZero

// Creates and initializes the target Array.
let myArrLen = [| 4 |]
let myArrLow = [| 2 |]
let myArrayTwo = Array.CreateInstance(typeof<string>, myArrLen, myArrLow)
myArrayTwo.SetValue("two", 2)
myArrayTwo.SetValue("three", 3)
myArrayTwo.SetValue("four", 4)
myArrayTwo.SetValue("five", 5)

// Displays the target Array.
printfn "The array with lower bound=2 contains:"
printIndexAndValues myArrayTwo

// Copies from the array with lower bound=0 to the array with lower bound=2.
myArrayZero.CopyTo(myArrayTwo, 3)

// Displays the modified target Array.
printfn "\nAfter copying to the target array from index 3:"
printIndexAndValues myArrayTwo


// This code produces the following output.
//     The array with lower bound=0 contains:
//         [0]:    zero
//         [1]:    one
//         [2]:
//     The array with lower bound=2 contains:
//         [2]:    two
//         [3]:    three
//         [4]:    four
//         [5]:    five
//     
//     After copying to the target array from index 3:
//         [2]:    two
//         [3]:    zero
//         [4]:    one
//         [5]:
using System;

public class SamplesArray2
{

   public static void Main()
   {
      // Creates and initializes the source Array.
      Array myArrayZero=Array.CreateInstance(typeof(string), 3);
      myArrayZero.SetValue("zero", 0);
      myArrayZero.SetValue("one", 1);

      // Displays the source Array.
      Console.WriteLine("The array with lower bound=0 contains:");
      PrintIndexAndValues(myArrayZero);

      // Creates and initializes the target Array.
      int[] myArrLen = { 4 };
      int[] myArrLow = { 2 };
      Array myArrayTwo=Array.CreateInstance(typeof(string), myArrLen, myArrLow);
      myArrayTwo.SetValue("two", 2);
      myArrayTwo.SetValue("three", 3);
      myArrayTwo.SetValue("four", 4);
      myArrayTwo.SetValue("five", 5);

      // Displays the target Array.
      Console.WriteLine("The array with lower bound=2 contains:");
      PrintIndexAndValues(myArrayTwo);

      // Copies from the array with lower bound=0 to the array with lower bound=2.
      myArrayZero.CopyTo(myArrayTwo, 3);

      // Displays the modified target Array.
      Console.WriteLine("\nAfter copying to the target array from index 3:");
      PrintIndexAndValues(myArrayTwo);
   }

   public static void PrintIndexAndValues(Array myArray)
   {
      for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++)
         Console.WriteLine("\t[{0}]:\t{1}", i, myArray.GetValue(i));
   }
}
/*
This code produces the following output.

The array with lower bound=0 contains:
    [0]:    zero
    [1]:    one
    [2]:
The array with lower bound=2 contains:
    [2]:    two
    [3]:    three
    [4]:    four
    [5]:    five

After copying to the target array from index 3:
    [2]:    two
    [3]:    zero
    [4]:    one
    [5]:
*/
Public Class SamplesArray2    
    
    Public Shared Sub Main()
        ' Creates and initializes the source Array.
        Dim myArrayZero As Array = Array.CreateInstance(GetType(String), 3)
        myArrayZero.SetValue("zero", 0)
        myArrayZero.SetValue("one", 1)
        
        ' Displays the source Array.
        Console.WriteLine("The array with lower bound=0 contains:")
        PrintIndexAndValues(myArrayZero)
        
        ' Creates and initializes the target Array.
        Dim myArrLen As Integer() = {4}
        Dim myArrLow As Integer() = {2}
        Dim myArrayTwo As Array = Array.CreateInstance(GetType(String), _
           myArrLen, myArrLow)
        myArrayTwo.SetValue("two", 2)
        myArrayTwo.SetValue("three", 3)
        myArrayTwo.SetValue("four", 4)
        myArrayTwo.SetValue("five", 5)
        
        ' Displays the target Array.
        Console.WriteLine("The array with lower bound=2 contains:")
        PrintIndexAndValues(myArrayTwo)
        
        ' Copies from the array with lower bound=0 to the array with lower bound=2.
        myArrayZero.CopyTo(myArrayTwo, 3)
        
        ' Displays the modified target Array.
        Console.WriteLine(ControlChars.Cr + "After copying to the target array from " _
           + "index 3:")
        PrintIndexAndValues(myArrayTwo)
    End Sub
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' This code produces the following output.
' 
' The array with lower bound=0 contains:
'     [0]:    zero
'     [1]:    one
'     [2]:    
' The array with lower bound=2 contains:
'     [2]:    two
'     [3]:    three
'     [4]:    four
'     [5]:    five
' 
' After copying to the target array from index 3:
'     [2]:    two
'     [3]:    zero
'     [4]:    one
'     [5]:

CopyTo(Array, Int32)

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將目前一維陣列的所有項目複製到指定的一維陣列 (從指定的目的陣列索引開始)。 索引已指定為 32 位元整數。

public:
 virtual void CopyTo(Array ^ array, int index);
public void CopyTo (Array array, int index);
public virtual void CopyTo (Array array, int index);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit
Public Sub CopyTo (array As Array, index As Integer)
Public Overridable Sub CopyTo (array As Array, index As Integer)

參數

array
Array

一維陣列,從目前陣列複製過來的項目之目的端。

index
Int32

32 位元的整數,代表 array 中的索引,由此開始複製。

實作

例外狀況

arraynull

index 小於 array 的下限。

array 是多維的。

-或-

來源陣列中的項目數大於從 index 到目的 array 結尾的可用項目數。

來源 Array 的類型無法自動轉換成目的 array 的類型。

該來源陣列為多維。

來源 Array 中至少有一個項目無法轉換成目的 array 的類型。

備註

這個方法會從索引 index 開始,將目前陣列實例的所有專案複製到 array 目的地陣列。 array目的陣列必須已經經過維度,而且必須有足夠的元素數目,才能容納複製的專案。 否則,方法會擲回例外狀況。

這個方法支援 System.Collections.ICollection 介面。 如果未明確要求實作 System.Collections.ICollection ,請使用 Copy 來避免額外的間接存取。

如果此方法在複製時擲回例外狀況,則 的狀態 array 為未定義。

這個方法是 O (n) 作業,其中 nLength 。 它只會執行淺層複製。

另請參閱

適用於

CopyTo(Array, Int64)

來源:
Array.cs
來源:
Array.cs
來源:
Array.cs

將目前一維陣列的所有項目複製到指定的一維陣列 (從指定的目的陣列索引開始)。 索引已指定為 64 位元整數。

public:
 void CopyTo(Array ^ array, long index);
public:
 virtual void CopyTo(Array ^ array, long index);
public void CopyTo (Array array, long index);
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void CopyTo (Array array, long index);
[System.Runtime.InteropServices.ComVisible(false)]
public void CopyTo (Array array, long index);
member this.CopyTo : Array * int64 -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member CopyTo : Array * int64 -> unit
override this.CopyTo : Array * int64 -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.CopyTo : Array * int64 -> unit
Public Sub CopyTo (array As Array, index As Long)
Public Overridable Sub CopyTo (array As Array, index As Long)

參數

array
Array

一維陣列,從目前陣列複製過來的項目之目的端。

index
Int64

64 位元的整數,代表 array 中的索引,由此開始複製。

屬性

例外狀況

arraynull

index 超出 array 的有效索引範圍。

array 是多維的。

-或-

來源陣列中的項目數大於從 index 到目的 array 結尾的可用項目數。

來源 Array 的類型無法自動轉換成目的 array 的類型。

來源 Array 是多維的。

來源 Array 中至少有一個項目無法轉換成目的 array 的類型。

備註

這個方法會從索引 index 開始,將目前陣列實例的所有專案複製到 array 目的地陣列。 array目的陣列必須已經經過維度,而且必須有足夠的元素數目,才能容納複製的專案。 否則,方法會擲回例外狀況。

這個方法支援 System.Collections.ICollection 介面。 如果未明確要求實作 System.Collections.ICollection ,請使用 Copy 來避免額外的間接存取。

如果此方法在複製時擲回例外狀況,則 的狀態 array 為未定義。

這個方法是 O (n) 作業,其中 nLength 。 它只會執行淺層複製。

另請參閱

適用於