快速完整性检查。 Array.Copy 仅适用于一维数组,还是可以处理更多?
在阅读规范时,我读到(可能是错误的)意思是匹配必须发生在第二个维度上。 IE。源索引和目标索引可以不同,我们可以指定元素的数量。
好吧,让我们开始研究“匹配”。对于一个 2 阶数组被复制到另一个 2 阶数组,比如 10x4 到 20x4,我们是说我们必须在一维和二维上匹配,还是仅在二维上匹配。我需要它只在第二个维度上。
是.net 中使用循环机制实现此目的的唯一方法。如果是这样,那将比 block 复制慢很多。
检查一下:
static void Main(string[] args)
{
var A = new int[2,4] {{1, 1, 1, 1}, {2, 2, 2, 2}};
var B = new int[4,4];
var C = new int[4, 4];
Array.Copy(A, 0, B, 4, 8);
Array.ConstrainedCopy(A, 0, C, 8, 8); // haven't noticed this method before. Same behaviour though.
}
请注意,我们必须在一维世界 View 中指定正确的起点。我没想到会这样。
请您参考如下方法:
引自 here :
When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).
