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。基本上,如果调整大小的数组更大,它只会在边缘添加空白矩形。如果它较小,那么它应该只切掉边缘。
执行此操作时:
Array.Copy(tiles, newArray, newWidth * newHeight);
它会弄乱数组,其所有内容都会变得无序,并且不会保留其原始索引。也许我只是脑子有病什么的?
This code works fine:
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];
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.
When doing this:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。然而,它并不像你想象的那样工作。相反,它将每个多维数组视为一个单维数组(这实际上是它们在内存中的样子,这只是一个技巧,让我们在它们之上放置一些结构以将它们视为多维),然后复制单个数组维度结构。因此,如果您有
并且想要将其复制到其中
,那么它将认为第一个数组为
,第二个数组为
,结果将是
,您将看到“
明白了吗?”
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
and want to copy it into
then it will think of the first array as
and the second as
and the result will be
which will appear to you as
Got it?
我使用这段代码:
像这样调用字符串数组
I use this code:
calling like this for array of strings
我需要在下一次中断命中之前消耗缓冲区中的数据并复制到大型保存数组。循环复制不是一个选择;太慢了。在完成所有复制之前,我不需要组合数据的多维结构,这意味着我可以 Buffer.BlockCopy() 到一维数组,然后再次复制到多维数组以获得所需的结构。这里有一些代码(在控制台中运行)将演示该技术以及性能。
输出:
I had a need to consume data from a buffer and copy off to a large holding array before the next interrupt hit. Copying in a loop wasn't an option; far too slow. I didn't need the multidimensional structure of the combined data until all of the copying was done, this meant I could Buffer.BlockCopy() to a single dimension array, then copy again onto a multidimensional array to obtain the required structure. Here's some code (run it in a console) that will demonstrate the technique as well as the performance.
Output:
简单地使用“Clone()”函数,如下所示:
这是您的数组列表
当您创建另一个数组时,只需使用以下代码:
简单!玩得开心!
Simple use the "Clone()" function like the following:
This is your array list
When you are creating another Array just use this code:
Simple! Have fun!