次の方法で共有


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

指定したコピー元インデックスを開始位置として Array から要素の範囲をコピーし、指定したコピー先インデックスを開始位置として他の Array にそれらの要素を貼り付けます。長さとインデックスは、32 ビット整数として指定します。

Overloads Public Shared Sub Copy( _
   ByVal sourceArray As Array, _   ByVal sourceIndex As Integer, _   ByVal destinationArray As Array, _   ByVal destinationIndex As Integer, _   ByVal length As Integer _)
[C#]
public static void Copy(ArraysourceArray,intsourceIndex,ArraydestinationArray,intdestinationIndex,intlength);
[C++]
public: static void Copy(Array* sourceArray,intsourceIndex,Array* destinationArray,intdestinationIndex,intlength);
[JScript]
public static function Copy(
   sourceArray : Array,sourceIndex : int,destinationArray : Array,destinationIndex : int,length : int);

パラメータ

  • sourceArray
    コピーするデータを格納している Array
  • sourceIndex
    コピーを開始する sourceArray のインデックスを表す 32 ビット整数。
  • destinationArray
    データを受け取る Array
  • destinationIndex
    格納を開始する destinationArray のインデックスを表す 32 ビット整数。
  • length
    コピーする要素の数を表す 32 ビット整数。

例外

例外の種類 条件
ArgumentNullException sourceArray が null 参照 (Visual Basic では Nothing) です。

または

destinationArray が null 参照 (Nothing) です。

RankException sourceArraydestinationArray のランクが異なります。
ArrayTypeMismatchException sourceArraydestinationArray の型に互換性がありません。
InvalidCastException sourceArray 内の 1 つ以上の要素を destinationArray の型にキャストできません。
ArgumentOutOfRangeException sourceIndex が、 sourceArray の最初の次元の下限より小さい値です。

または

destinationIndex が、 destinationArray の最初の次元の下限より小さい値です。

または

length が 0 未満です。

ArgumentException length が、 sourceIndex から sourceArray の末尾までの要素数を超えています。

または

length が、 destinationIndex から destinationArray の末尾までの要素数を超えています。

解説

sourceArray パラメータと destinationArray パラメータは、同じ次元数である必要があります。

多次元配列間でコピーする場合、概念的には、配列は各行 (または列) の先頭と末尾をつなげて並べた長い 1 次元配列のように動作します。たとえば、配列内にそれぞれ 4 つ要素を持つ行 (または列) が 3 つある場合、配列の先頭から 6 つ要素をコピーすると、最初の行 (または列) の 4 つの要素すべてと、2 番目の行 (または列) の最初の 2 つの要素がコピーされます。3 番目の行 (または列) の 2 番目の要素からコピーを開始するには、 srcIndex で、最初の行 (または列) の上限 + 2 番目の行 (または列) の長さ + 2 の値を指定する必要があります。

sourceArraydestinationArray が重なっている場合、このメソッドは、 destinationArray が上書きされる前に sourceArray の元の値が一時的な場所に保存されたかのように動作します。

[C++] このメソッドは標準 C/C++ 関数 memmove と等価であり、 memcpy とは等価ではありません。

配列は参照型配列または値型配列のどちらかです。必要に応じて型のダウンキャストが実行されます。

  • 参照型配列から値型配列にコピーすると、各要素のボックス化が解除されてから、コピーされます。値型配列から参照型配列にコピーすると、各要素がボックス化されてから、コピーされます。
  • 参照型配列または値型配列から Object 配列にコピーすると、各値または参照を保持する Object が作成されてから、コピーされます。 Object 型の配列から参照型または値型の配列にコピーするときに、割り当てが不可能になる場合は、 InvalidCastException がスローされます。
  • sourceArraydestinationArray がどちらも参照型配列であるか、どちらも Object 型配列である場合は、簡易コピーが実行されます。 Array の簡易コピーは、元の Array と同じ要素への参照を含む新しい Array となります。要素自体または要素が参照する対象はコピーされません。対照的に、 Array の詳細コピーでは、要素自体および要素が直接的または間接的に参照するすべての対象がコピーされます。

配列が互換性のない型の場合は、 ArrayTypeMismatchException がスローされます。型互換性の定義を次に示します。

  • 各型はその型そのものと互換性があります。
  • 値型は、 Object 、およびこの値型によって実装されるインターフェイス型と互換性があります。値型は、インターフェイスを直接実装している場合にだけ、そのインターフェイスに結び付けられていると見なされます。接続されていない型は互換性がありません。
  • 2 つの組み込み (定義済み) 値型は、コピー元の型からコピー先の型へのコピーが拡大変換である場合に、互換性があります。拡大変換では情報が失われませんが、縮小変換では情報が失われることがあります。たとえば、32 ビット符号付き整数から 64 ビット符号付き整数への変換は拡大変換であり、64 ビット符号付き整数から 32 ビット符号付き整数への変換は縮小変換です。変換の詳細については、 Convert のトピックを参照してください。
  • 非組み込み (ユーザー定義の) 値型は、その値型そのものとだけ互換性があります。

sourceArray 内のすべての要素のダウンキャスト (基本クラスから派生クラスへのキャスト、インターフェイスからオブジェクトへのキャストなど) が必要な場合に、1 つ以上の要素を destinationArray 内の対応する型にキャストできないときは、 InvalidCastException がスローされます。

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

使用例

Object 型の Array から整数型の他の Array にコピーする方法を次のコード例に示します。

 
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array of type Int32.
        Dim myIntArray As Array = _
           Array.CreateInstance(Type.GetType("System.Int32"), 5)
        Dim i As Integer
        For i = myIntArray.GetLowerBound(0) To myIntArray.GetUpperBound(0)
            myIntArray.SetValue(i + 1, i)
        Next i 
        ' Creates and initializes a new Array of type Object.
        Dim myObjArray As Array = _
           Array.CreateInstance(Type.GetType("System.Object"), 5)
        For i = myObjArray.GetLowerBound(0) To myObjArray.GetUpperBound(0)
            myObjArray.SetValue(i + 26, i)
        Next i 
        ' Displays the initial values of both arrays.
        Console.WriteLine("Int32 array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array:")
        PrintValues(myObjArray)
        
        ' Copies the first element from the Int32 array to the Object array.
        Array.Copy(myIntArray, myIntArray.GetLowerBound(0), myObjArray, _
           myObjArray.GetLowerBound(0), 1)
        
        ' Copies the last two elements from the Object array to the Int32 array.
        Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, _
           myIntArray.GetUpperBound(0) - 1, 2)
        
        ' Displays the values of the modified arrays.
        Console.WriteLine("Int32 array - Last two elements should now be " _
           + "the same as Object array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array - First element should now be the " _
           + "same as Int32 array:")
        PrintValues(myObjArray)
    End Sub
    
    Public Shared Sub PrintValues(myArr As Array)
        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(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Int32 array:
'     1    2    3    4    5
' Object array:
'     26    27    28    29    30
' Int32 array - Last two elements should now be the same as Object array:
'     1    2    3    29    30
' Object array - First element should now be the same as Int32 array:
'     1    27    28    29    30

[C#] 
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array of type Int32.
      Array myIntArray=Array.CreateInstance( Type.GetType("System.Int32"), 5 );
      for ( int i = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ )
         myIntArray.SetValue( i+1, i );

      // Creates and initializes a new Array of type Object.
      Array myObjArray = Array.CreateInstance( Type.GetType("System.Object"), 5 );
      for ( int i = myObjArray.GetLowerBound(0); i <= myObjArray.GetUpperBound(0); i++ )
         myObjArray.SetValue( i+26, i );

      // Displays the initial values of both arrays.
      Console.WriteLine( "Int32 array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array:" );
      PrintValues( myObjArray );

      // Copies the first element from the Int32 array to the Object array.
      Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 );

      // Copies the last two elements from the Object array to the Int32 array.
      Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

      // Displays the values of the modified arrays.
      Console.WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array - First element should now be the same as Int32 array:" );
      PrintValues( myObjArray );
   }


   public static 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( "\t{0}", myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/*
This code produces the following output.

Int32 array:
    1    2    3    4    5
Object array:
    26    27    28    29    30
Int32 array - Last two elements should now be the same as Object array:
    1    2    3    29    30
Object array - First element should now be the same as Int32 array:
    1    27    28    29    30
*/

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

void PrintValues( Array* myArr );

void main()  {
 
       // Creates and initializes a new Array instance of type Int32.
       Array* myIntArray=Array::CreateInstance( Type::GetType("System.Int32"), 5 );
       for ( int i = myIntArray->GetLowerBound(0); i <= myIntArray->GetUpperBound(0); i++ )
          myIntArray->SetValue( __box(i+1), i );
 
       // Creates and initializes a new Array instance of type Object.
       Array* myObjArray = Array::CreateInstance( Type::GetType("System.Object"), 5 );
       for ( int i = myObjArray->GetLowerBound(0); i <= myObjArray->GetUpperBound(0); i++ )
          myObjArray->SetValue( __box(i+26), i );
 
       // Displays the initial values of both arrays.
       Console::WriteLine( "Int32 array:" );
       PrintValues( myIntArray );
       Console::WriteLine( "Object array:" );
       PrintValues( myObjArray );
 
       // Copies the first element from the Int32 array to the Object array.
       Array::Copy( myIntArray, myIntArray->GetLowerBound(0), myObjArray, myObjArray->GetLowerBound(0), 1 );
 
       // Copies the last two elements from the Object array to the Int32 array.
       Array::Copy( myObjArray, myObjArray->GetUpperBound(0) - 1, myIntArray, myIntArray->GetUpperBound(0) - 1, 2 );
 
       // Displays the values of the modified arrays.
       Console::WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
       PrintValues( myIntArray );
       Console::WriteLine( "Object array - First element should now be the same as Int32 array:" );
       PrintValues( myObjArray );
    }
 
 
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( "\t{0}", myEnumerator->Current );
    }
    Console::WriteLine();
}

 /*
 This code produces the following output.
 
 Int32 array:
     1    2    3    4    5
 Object array:
     26    27    28    29    30
 Int32 array - Last two elements should now be the same as Object array:
     1    2    3    29    30
 Object array - First element should now be the same as Int32 array:
     1    27    28    29    30
 */

[JScript] 
import System;

// Creates and initializes a new System.Array of type Int32.
var myIntArray : System.Array = System.Array.CreateInstance( Type.GetType("System.Int32"), 5 );
for ( var i : int = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ )
  myIntArray.SetValue( Int32(i+1), i );

// Creates and initializes a new Array of type Object.
var myObjArray : System.Array = System.Array.CreateInstance( Type.GetType("System.Object"), 5 );
for ( var j : int = myObjArray.GetLowerBound(0); j <= myObjArray.GetUpperBound(0); j++ )
  myObjArray.SetValue( Int32(j+26), j);

// Displays the initial values of both arrays.
Console.WriteLine( "Int32 array:" );
PrintValues( myIntArray );
Console.WriteLine( "Object array:" );
PrintValues( myObjArray );

// Copies the first element from the Int32 array to the Object array.
System.Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 );

// Copies the last two elements from the Object array to the Int32 array.
System.Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

// Displays the values of the modified arrays.
Console.WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
PrintValues( myIntArray );
Console.WriteLine( "Object array - First element should now be the same as Int32 array:" );
PrintValues( myObjArray );
 
 
function PrintValues( myArr : System.Array )  {
   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( "\t{0}", myEnumerator.Current );
   }
   Console.WriteLine();
}
 /*
 This code produces the following output.
 
 Int32 array:
     1    2    3    4    5
 Object array:
     26    27    28    29    30
 Int32 array - Last two elements should now be the same as Object array:
     1    2    3    29    30
 Object array - First element should now be the same as Int32 array:
     1    27    28    29    30
 */

必要条件

プラットフォーム: 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.Copy オーバーロードの一覧