Skip to main content
 首页 » 编程设计

c#之将 bool 的多维数组复制到另一个数组中

2024年02月13日17虾米姐

如何将 bool 的多维数组复制到其他数组中。一维数组使用长度,但我不确定这是否适用于多维数组。

bool[][] OldArray 
bool[][] NewArray = new bool[][]; 
 
Array.Copy(NewArray,OldArray,NewArray.length) 

请您参考如下方法:

那不是多维数组,而是锯齿状数组(数组的数组)。

bool[,] multiDimArray; 

是多维的

bool[][] jaggedArray; 

是锯齿状的。不同之处在于,多维数组始终为矩形,而锯齿状数组则不一定。

要复制多维数组(第一种情况),您只需将每个维度的长度相乘,因为(来自 MSDN):

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).

对于锯齿形,您需要遍历第一个维度,然后依次Array.Copy 每个内部数组。