将一个锯齿状数组复制到另一个锯齿状数组之上
我怎样才能完成将一个锯齿状数组复制到另一个锯齿状数组?例如,我有一个 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
和一个 4x3 数组:
0,1,1,0
1,1,1,1
0,1,1,0
我希望能够在全零数组上指定特定的起点,例如 (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
最好的方法是什么?
How could I accomplish copying one jagged array to another? For instance, I have a 5x7 array of:
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:
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于示例的平方性质,这似乎更适合二维数组而不是锯齿状数组。但无论哪种方式,你当然可以用老式的方式来完成它并循环它。像(未经测试)
编辑:像马克一样,我也有一些轻微的改进,略有不同,但大致相同。
Due to the squared nature of your example, this seems more fitting of a 2D array instead of jagged. But either way, you could certainly do it the old fashioned way and loop over it. Something like (untested)
Edit: Like Mark, I also had a slight improvement, slightly different but along the same lines.
即使您的输入不是矩形,这也应该起作用:
This should work even if your inputs are not rectangular: