次の方法で共有


Array.CopyTo メソッド (Array, Int32)

指定したコピー先の Array インデックスを開始位置として、現在の 1 次元 Array のすべての要素を指定した 1 次元 Array にコピーします。インデックスは 32 ビット整数値として指定します。

Overloads Public Overridable Sub CopyTo( _
   ByVal array As Array, _   ByVal index As Integer _) Implements ICollection.CopyTo
[C#]
public virtual void CopyTo(Arrayarray,intindex);
[C++]
public: virtual void CopyTo(Array* array,intindex);
[JScript]
public function CopyTo(
   array : Array,index : int);

パラメータ

  • array
    現在の Array から要素がコピーされる先の 1 次元の Array
  • index
    コピーを開始する array のインデックスを表す 32 ビット整数。

実装

ICollection.CopyTo

例外

例外の種類 条件
ArgumentNullException array が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException index が array の下限より小さい値です。
ArgumentException コピー元の Array が多次元です。

または

array が多次元です。

または

index が array の長さ以上です。

または

コピー元の Array の要素数が、 index からコピー先の array の末尾までに格納できる数を超えています。

ArrayTypeMismatchException コピー元の Array の型が、コピー先の array の型に自動的にキャストできません。

解説

このメソッドは、 System.Collections.ICollection インターフェイスをサポートします。 System.Collections.ICollection の実装が明示的に必要ではない場合は、 Copy を使用して余分な間接処理を回避します。

このメソッドがコピー中に例外をスローする場合は、array の状態が未定義です。

使用例

Array を他の Array にコピーする方法を次のコード例に示します。

 
Imports System
Imports Microsoft.VisualBasic

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

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

[C++] 
#using <mscorlib.dll>
using namespace System;

void main1();
void main2();

void main() {

    main1();
    
    Console::WriteLine();

    main2();
}

void PrintValues( Array* myArr );

    void main1()  {
 
       // Creates and initializes two new Array instances.
       Array* mySourceArray = Array::CreateInstance( __typeof(String), 6 );

       mySourceArray->SetValue( S"three", 0 );
       mySourceArray->SetValue( S"napping", 1 );
       mySourceArray->SetValue( S"cats", 2 );
       mySourceArray->SetValue( S"in", 3 );
       mySourceArray->SetValue( S"the", 4 );
       mySourceArray->SetValue( S"barn", 5 );

       Array* myTargetArray=Array::CreateInstance( __typeof(String), 15 );

       myTargetArray->SetValue( S"The", 0 );
       myTargetArray->SetValue( S"quick", 1 );
       myTargetArray->SetValue( S"brown", 2 );
       myTargetArray->SetValue( S"fox", 3 );
       myTargetArray->SetValue( S"jumped", 4 );
       myTargetArray->SetValue( S"over", 5 );
       myTargetArray->SetValue( S"the", 6 );
       myTargetArray->SetValue( S"lazy", 7 );
       myTargetArray->SetValue( S"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 jumped over the lazy dog      
  The quick brown fox jumped over three napping cats in the barn
 */

[JScript] 
import System;
public class SamplesArray  {

   public static function Main()  {

      // Creates and initializes two new Arrays.
      var mySourceArray : System.Array = System.Array.CreateInstance( System.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 );
      var myTargetArray : System.Array = System.Array.CreateInstance( System.String, 15 );
      myTargetArray.SetValue( "The", 0 );
      myTargetArray.SetValue( "quick", 1 );
      myTargetArray.SetValue( "brown", 2 );
      myTargetArray.SetValue( "fox", 3 );
      myTargetArray.SetValue( "jumped", 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 function PrintValues( myArr : System.Array , mySeparator : char  )  {
      var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator();
      var i : int= 0;
      var cols : int = 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 jumped over the lazy dog      
 The quick brown fox jumped over three napping cats in the barn
*/

0 以外の下限を指定して、 Array を他の Array にコピーする方法を次のコード例に示します。コピー先 Array 内の既存の要素が空の要素で上書きされることを含め、コピー元 Array の全体がコピーされます。

 
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]: 

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

[C++] 

    void PrintIndexAndValues( Array* myArray );
 
    void main2()  {
       // Creates and initializes the source Array.
       Array* myArrayZero = Array::CreateInstance( __typeof(String), 3 );
       myArrayZero->SetValue( S"zero", 0 );
       myArrayZero->SetValue( S"one", 1 );
 
       // Displays the source Array.
       Console::WriteLine( "The array with lowbound=0 contains:" );
       PrintIndexAndValues( myArrayZero );
 
       // Creates and initializes the target Array.
       int myArrLen __gc[]= { 4 };
       int myArrLow __gc[]= { 2 };
       Array* myArrayTwo = Array::CreateInstance( __typeof(String), myArrLen, myArrLow );
       myArrayTwo->SetValue( S"two", 2 );
       myArrayTwo->SetValue( S"three", 3 );
       myArrayTwo->SetValue( S"four", 4 );
       myArrayTwo->SetValue( S"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}", __box(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]:
 */

[JScript] 
public class SamplesArray2{

   public static function Main()  {
      // Creates and initializes the source Array.
      var myArrayZero : System.Array = System.Array.CreateInstance( System.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.
      var myArrLen : int[] = [4];
      var myArrLow : int[] = [2];
      var myArrayTwo : System.Array = System.Array.CreateInstance( System.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 function PrintIndexAndValues( myArray : System.Array )  {
      for ( var i : int = 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]:
*/

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Array クラス | Array メンバ | System 名前空間 | Array.CopyTo オーバーロードの一覧 | ICollection | Copy