获取一个double [,]矩形阵列的双[]排阵矩形、阵列、double

2023-09-03 20:55:26 作者:坏小子帅的滴渣

假设你有一个像数组:

double[,] rectArray = new double[10,3];

现在你想要的fouth行作为3个元素的双重[]数组没有这样做的:

Now you want the fouth row as a double[] array of 3 elements without doing:

double[] fourthRow = new double[]{rectArray[3,0],
                                  rectArray[3,1], 
                                  rectArray[3,2]};

是否有可能好歹?即使使用Marshal.Something方法?

Is it possible someway? Even using a Marshal.Something approach?

谢谢!

推荐答案

LINQ救援:

var s = rectArray.Cast<double>().Skip(9).Take(3).ToArray();

说明:铸造一多维数组变平并将它与单个二维数组。之后,我们需要做的就是跳到我们想要的元素(2-D数组中的第4个元素解析为跳过(9)...),并采取3个元素,从它)。

Explanation: Casting a multi-dimensional array flattens it to a single-dimensional array. After that all we need to do is skip to the element we want (the 4th element in the 2-D array resolves to Skip(9)...) and take 3 elements from it).