简体   繁体   English

将一个锯齿状阵列复制到另一个上面

[英]Copy one jagged array ontop of another

How could I accomplish copying one jagged array to another? 我怎样才能完成将一个锯齿状阵列复制到另一个? For instance, I have a 5x7 array of: 例如,我有一个5x7阵列:

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 of: 和4x3阵列:

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

I would like to be able to specify a specific start point such as (1,1) on my all zero array, and copy my second array ontop of it so I would have a result such as: 我希望能够在我的所有零数组上指定一个特定的起始点,如(1,1),并复制我的第二个数组,所以我会得到如下结果:

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? 最好的方法是什么?

Due to the squared nature of your example, this seems more fitting of a 2D array instead of jagged. 由于您的示例的平方性质,这似乎更适合2D数组而不是锯齿状。 But either way, you could certainly do it the old fashioned way and loop over 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];
    }
}

Edit: Like Mark, I also had a slight improvement, slightly different but along the same lines. 编辑:像马克一样,我也略有改进,略有不同,但沿着同样的路线。

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

This should work even if your inputs are not rectangular: 即使输入不是矩形,这也应该有效:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM