Array.ConstrainedCopy() 方法
Array.ConstrainedCopy() 方法用于从数组中复制一系列元素,它从指定源索引开始,将它们粘贴到另一个,从指定目标索引开始。
语法
public static void ConstrainedCopy (Array sourceArr, int sourceIndex, Array destinationArr, int destinationIndex, int length);
此处,
sourceArr - 包含要复制的数据的数组。
sourceIndex - 整数,表示 sourceArr 中开始复制的索引。
destinationArr - 接收数据的数组。
destinationIndex - 整数,表示 destinationArr 中开始存储的索引。
len - 整数,表示要复制的元素数量。
现在我们提供一个示例来实现 Array.ConstrainedCopy() 方法 -
示例
using System;
public class Demo{
public static void Main(){
int[] arrDest = new int[10];
Console.WriteLine("Array elements...");
int[] arrSrc = { 20, 50, 100, 150, 200, 300, 400};
for (int i = 0; i < arrSrc.Length; i++){
Console.Write("{0} ", arrSrc[i]);
}
Console.WriteLine();
Array.ConstrainedCopy(arrSrc, 3, arrDest, 0, 4);
Console.WriteLine("Destination Array: ");
for (int i = 0; i < arrDest.Length; i++){
Console.Write("{0} ", arrDest[i]);
}
}
}输出
这将产生以下输出 -
Array elements... 20 50 100 150 200 300 400 Destination Array: 150 200 300 400 0 0 0 0 0 0
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP