ste*_*ell 16 .net c# arrays multidimensional-array
我们有以下对象
int [,] oGridCells;
Run Code Online (Sandbox Code Playgroud)
它仅用于固定的第一个索引
int iIndex = 5;
for (int iLoop = 0; iLoop < iUpperBound; iLoop++)
{
//Get the value from the 2D array
iValue = oGridCells[iIndex, iLoop];
//Do something with iValue
}
Run Code Online (Sandbox Code Playgroud)
在.NET中是否有办法将固定的第一个索引处的值转换为单个维度数组(除了通过循环值)?
如果数组只循环一次,我怀疑它会加速代码(并且可能会使代码变慢).但是如果数组被大量操作,那么单维数组比多维数组更有效.
我提出问题的主要原因是看它是否可以完成以及如何完成,而不是将其用于生产代码.
Blu*_*kMN 29
以下代码演示了将16字节(4个整数)从2-D阵列复制到1-D阵列.
int[,] oGridCells = {{1, 2}, {3, 4}};
int[] oResult = new int[4];
System.Buffer.BlockCopy(oGridCells, 0, oResult, 0, 16);
Run Code Online (Sandbox Code Playgroud)
您还可以通过提供正确的字节偏移量,从阵列中选择性地仅复制一行.此示例复制3行2-D数组的中间行.
int[,] oGridCells = {{1, 2}, {3, 4}, {5, 6}};
int[] oResult = new int[2];
System.Buffer.BlockCopy(oGridCells, 8, oResult, 0, 8);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43211 次 |
| 最近记录: |