是否Array.Copy与多维数组工作? [英] Does Array.Copy work with multidimensional arrays?

查看:169
本文介绍了是否Array.Copy与多维数组工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码工作正常:

var newArray = new Rectangle[newHeight, newWidth];

for (int x = 0; x < newWidth; x++)
    for (int y = 0; y < newHeight; y++)
        newArray[y, x] = (x >= width) || (y >= height) ? Rectangle.Empty : tiles[y, x];



但我没有多少运气Array.Copy取代它。基本上,如果调整大小的数组较大它只是增加了空白矩形的边缘。如果是面积较小的话,应该只是切断边

But I am not having much luck replacing it with Array.Copy. Basically, if the resized array is larger it just adds blank rectangles to the edges. If it is smaller then it should just cut off the edges.

在做这样的:

Array.Copy(瓷砖,newArray,newWidth * newHeight);

它弄乱了阵列和它的所有内容变得紊乱不要保留其原有的指标。也许我只是有一个brainfart什么?

It messes up the array and all of its contents become disordered and do not retain their original index. Maybe I'm just having a brainfart or something?

推荐答案

是的。但是,它不工作,你在想它的工作方式。相反,它认为每个mutlidimensional数组作为一维数组(实际上是它们在内存中的东西,它只是一个把戏,让我们将一些结构在上面把它们多维的),然后复制单维结构。所以,如果你有

Yes. However, it doesn't work the way you are thinking it works. Rather, it thinks of each mutlidimensional array as a single-dimensional array (which is actually what they are in memory, it's just a trick that lets us place some structure on top of them to think of them as multidimensional) and then copies the single-dimensional structures. So if you have

1 2 3
4 5 6

和希望将它复制到

x x x x
x x x x

那么它会认为第一个数组为

then it will think of the first array as

1 2 3 4 5 6

和第二个为

x x x x x x x x

和结果将是

1 2 3 4 5 6 x x

将出现给您

1 2 3 4
5 6 x x

明白了吗?

这篇关于是否Array.Copy与多维数组工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆