Array.Copy【转】

1,从第一个元素开始复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从第一个元素开始)。长度指定为 32 位整数。

Visual Basic(声明) 
Public
 Shared
 Sub
 Copy
 ( _
    sourceArray
 As
 Array
, _
    destinationArray
 As
 Array
, _
    length
 As
 Integer
 _
)
 
Visual Basic(用法) 
Dim
 sourceArray
 As
 Array

Dim
 destinationArray
 As
 Array

Dim
 length
 As
 Integer


Array
.Copy
(sourceArray
, destinationArray
, _
    length
)
 
 
public
 static
 void
 Copy
(
    Array
 sourceArray
,
    Array
 destinationArray
,
    int
 length

)

参数
sourceArray
类型:System. Array 

Array ,它包含要复制的数据。

destinationArray
类型:System. Array 

Array ,它接收数据。

length
类型:System. Int32 

一个 32 位整数,它表示要复制的元素数目

2,从第一个元素开始复制 Array
 中的一系列元素,将它们粘贴到另一 Array
 中(从第一个元素开始)。长度指定为 64 位整数。
public
 static
 void
 Copy
(
    Array
 sourceArray
,
    Array
 destinationArray
,
    long
 length

)

参数
sourceArray
类型:System. Array 

Array ,它包含要复制的数据。

destinationArray
类型:System. . Array 

Array ,它接收数据。

length
类型:System. Int64 

一个 64 位整数,它表示要复制的元素数目。该整数必须介于零和 Int32. MaxValue 之间(包括零和 Int32. MaxValue )。

3,从指定的源索引开始,复制 Array
 中的一系列元素,将它们粘贴到另一 Array
 中(从指定的目标索引开始)。长度和索引指定为 32 位整数。
public
 static
 void
 Copy
(
    Array
 sourceArray
,
    int
 sourceIndex
,
    Array
 destinationArray
,
    int
 destinationIndex
,
    int
 length

)

参数
sourceArray
类型:System. Array 

Array ,它包含要复制的数据。

sourceIndex
类型:System. Int32 

一个 32 位整数,它表示 sourceArray 中复制开始处的索引。

destinationArray
类型:System . Array 

Array ,它接收数据。

destinationIndex
类型:System. Int32 

一个 32 位整数,它表示 destinationArray 中存储开始处的索引。

length
类型:System. . :: . Int32 

一个 32 位整数,它表示要复制的元素数目。

 

4,从指定的源索引开始,复制 Array
 中的一系列元素,将它们粘贴到另一 Array
 中(从指定的目标索引开始)。长度和索引指定为 64 位整数。
public
 static
 void
 Copy
(
    Array
 sourceArray
,
    long
 sourceIndex
,
    Array
 destinationArray
,
    long
 destinationIndex
,
    long
 length

)

参数
sourceArray
类型:System. Array 

Array ,它包含要复制的数据。

sourceIndex
类型:System. Int64 

一个 64 位整数,它表示 sourceArray 中复制开始处的索引。

destinationArray
类型:System. Array 

Array ,它接收数据。

destinationIndex
类型:System. Int64 

一个 64 位整数,它表示 destinationArray 中存储开始处的索引。

length
类型:System. Int64 

一个 64 位整数,它表示要复制的元素数目。该整数必须介于零和 Int32. . MaxValue 之间(包括零和 Int32. MaxValue )。

 

5,下面的代码示例说明如何从一个 Object 类型的 Array 复制到 integer 类型的另一个 Array 。

using
 System;
public
 class
 SamplesArray  {

   public
 static
 void
 Main()  {

      // Creates and initializes a new Array of type Int32.

      Array myIntArray=Array.CreateInstance( typeof
(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( typeof
(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
*/



 

 

posted on 2012-11-05 22:30  。!  阅读(237)  评论(0编辑  收藏  举报