次の方法で共有


Array.Copy メソッド

一方の Array の範囲を他方の Array にコピーし、必要に応じて型キャストとボックス化を実行します。

オーバーロードの一覧

最初の要素を開始位置として Array から要素の範囲をコピーし、最初の要素を開始位置として他の Array にそれらの要素を貼り付けます。長さは 32 ビット整数値として指定します。

[Visual Basic] Overloads Public Shared Sub Copy(Array, Array, Integer)

[C#] public static void Copy(Array, Array, int);

[C++] public: static void Copy(Array*, Array*, int);

[JScript] public static function Copy(Array, Array, int);

最初の要素を開始位置として Array から要素の範囲をコピーし、最初の要素を開始位置として他の Array にそれらの要素を貼り付けます。長さは 64 ビット整数値として指定します。

[Visual Basic] Overloads Public Shared Sub Copy(Array, Array, Long)

[C#] public static void Copy(Array, Array, long);

[C++] public: static void Copy(Array*, Array*, __int64);

[JScript] public static function Copy(Array, Array, long);

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Sub Copy(Array, Integer, Array, Integer, Integer)

[C#] public static void Copy(Array, int, Array, int, int);

[C++] public: static void Copy(Array*, int, Array*, int, int);

[JScript] public static function Copy(Array, int, Array, int, int);

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

[Visual Basic] Overloads Public Shared Sub Copy(Array, Long, Array, Long, Long)

[C#] public static void Copy(Array, long, Array, long, long);

[C++] public: static void Copy(Array*, __int64, Array*, __int64, __int64);

[JScript] public static function Copy(Array, long, Array, long, long);

使用例

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
 */

参照

Array クラス | Array メンバ | System 名前空間