开发者

Add a dimension to a multidimensional array in c#

I have a multidimensional array

byte[,] matrix;

and i want copy in a 3 dimension array

byte[,,] 3dplan; 

in this way

3dplan[,,0]=matrix

What is the fastest way to accomplish this ta开发者_JAVA技巧sk in c#?


You need to manually copy the elements in a nested loop; there is no faster way.

If you switch to a jagged array (byte[,][] or byte[][][]), you can insert the smaller array as-is into a slot in the larger array (although they will both refer to the same array instance and will pick up changes)


You can copy 2d array into 3d array with Buffer.BlockCopy:

var _3dplan = new int[2, 2, 2];
var _2dplan = new int[2, 2] { { 1, 1 }, { 1, 1 } };
var index = 0;
var size = _2dplan.Length * sizeof(int);
Buffer.BlockCopy(_2dplan, 0, _3dplan, index * size, size);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜