Convert list <T> to array (multidimensional)
Possible duplicate:
How to convert the list <> to a multidimensional array?
I want to have an array in the form double[,]for this purpose, since I don't know what the length of this array will be, I want to do it first Listand then use List<T>.ToArray()convert it to double[,]:
public double[,] FilterClampedData(double[,] data)
{
var finalData = new List<double[]>();
//Do some stuff on parameter
return finalData.ToArray(); ///Does not compile :(
}
+5
3 answers
Since it ToArrayreturns a one-dimensional array, it is not surprising why this does not compile. If you return double[][], it will compile, however. You can also create your 2-dimensional array manually using two nested loops:
var R = finalData.Count;
var C = finalData[0].Length;
var res = new double[R, C];
for (int r = 0 ; r != R ; r++)
for (int c = 0 ; c != C ; c++)
res[r, c] = finalData[r][c];
return res;
, finalData finalData .
+5