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


转载于:https://www.cnblogs.com/cpcpc/archive/2010/07/19/2123112.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
np.ones和np.array都是NumPy库中的函数,但它们的功能和用法有所不同。 1. np.ones函数:np.ones(shape, dtype=None, order='C')返回一个指定形状(shape)和数据类型(dtype)的数组,数组中的所有元素都是1。 - shape:数组的形状,可以是整数或整数元组。 - dtype:可选参数,指定数组中元素的数据类型,默认为float64。 - order:可选参数,指定数组在内存中的存储顺序,默认为'C'(按行)。 例子: ```python import numpy as np arr = np.ones((3, 4), dtype=int) print(arr) # 输出: # [[1 1 1 1] # [1 1 1 1] # [1 1 1 1]] ``` 运用场景: - 创建一个指定形状的全1数组,用于初始化某些操作。 - 构造测试数据,用于验证算法或模型。 2. np.array函数:np.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)将输入数据(object)转换为一个ndarray对象(多维数组)。 - object:输入的数据,可以是列表、元组、其他数组等。 - dtype:可选参数,指定数组中元素的数据类型,默认根据输入数据推断得出。 - copy:可选参数,指定是否复制输入数据,默认为True(复制)。 - order:可选参数,指定数组在内存中的存储顺序,默认为'K'(保持输入数据的顺序)。 - subok:可选参数,指定是否返回子类对象,默认为False。 - ndmin:可选参数,指定输出数组的最小维度,默认为0(根据输入数据的维度确定)。 例子: ```python import numpy as np arr = np.array([1, 2, 3]) print(arr) # 输出:[1 2 3] ``` 运用场景: - 将其他数据类型(如列表、元组)转换为NumPy数组,便于进行数值计算和操作。 - 通过指定ndmin参数,可以创建具有指定维度的数组。 总结: np.ones主要用于创建指定形状的全1数组,而np.array用于将输入数据转换为NumPy数组。它们的主要区别在于功能和用法,适用于不同的场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值