Copy one gear array to another

How can I copy one gear array to another? For example, I have a 5x7 array:

0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0

and a 4x3 array:

0,1,1,0
1,1,1,1
0,1,1,0

I would like to specify a specific starting point, such as (1,1) for my entire zero array, and copy my second array to it so that I have the result, for example:

0, 0, 0, 0, 0, 0, 0
0, 0, 1, 1, 0, 0, 0
0, 1, 1, 1, 1, 0, 0
0, 0, 1, 1, 0, 0, 0
0, 0, 0, 0, 0, 0, 0

What would be the best way to do this?

+3
source share
2 answers

Due to the square nature of your example, this seems more appropriate for a 2D array instead of a jagged one. But in any case, you could do it the old-fashioned way and fixate on it. Something like (untested)

for (int i = 0; i < secondArray.Length; i++)
{
    for (int j = 0; j < secondArray[0].Length; j++)
    {
        firstArray[startingRow + i][startingColumn + j] = secondArray[i][j];
    }
}

: Mark, , , .

for (int i = 0; i < secondArray.Length; i++)
{
    secondArray[i].CopyTo(firstArray[startingRow + i], startingColumn);
}
+2

, :

void copy(int[][] source, int[][] destination, int startRow, int startCol)
{
    for (int i = 0; i < source.Length; ++i)
    {
        int[] row = source[i];
        Array.Copy(row, 0, destination[i + startRow], startCol, row.Length);
    }
}
+2

Source: https://habr.com/ru/post/1739241/


All Articles