How to copy a row of values from a 2D array into a 1D array?

前端 未结 6 2158
醉话见心
醉话见心 2020-12-05 08:17

We have the following object

int [,] oGridCells;

which is only used with a fixed first index

int iIndex = 5;
for (int iLoop         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 08:51

    The following code demonstrates copying 16 bytes (4 ints) from a 2-D array to a 1-D array.

    int[,] oGridCells = {{1, 2}, {3, 4}};
    int[] oResult = new int[4];
    System.Buffer.BlockCopy(oGridCells, 0, oResult, 0, 16);
    

    You can also selectively copy just 1 row from the array by providing the correct byte offsets. This example copies the middle row of a 3-row 2-D array.

    int[,] oGridCells = {{1, 2}, {3, 4}, {5, 6}};
    int[] oResult = new int[2];
    System.Buffer.BlockCopy(oGridCells, 8, oResult, 0, 8);
    

提交回复
热议问题