Array.Copy 是否适用于多维数组? [英] Does Array.Copy work with multidimensional arrays?

查看:39
本文介绍了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(tiles, newArray, newWidth * newHeight);

它弄乱了数组,它的所有内容都变得无序并且不保留其原始索引.也许我只是脑放屁什么的?

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?

推荐答案

是的.但是,它并不像您认为的那样工作.相反,它将每个多维数组视为一个一维数组(这实际上是它们在内存中的内容,这只是一个技巧,让我们在它们之上放置一些结构以将它们视为多维数组),然后复制单个数组维结构.所以如果你有

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天全站免登陆