Possible Duplicate:
How can I convert a list<> to a multi-dimensional array?
I want to have an array in form of double[,]
for this purpose since I do not know what will be the length of this array, I want to make a List
first then later using 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 :(
}
Since ToArray
returns a one-dimensional array, there is no wonder why this does not compile. If you were returning double[][]
, it would compile, however. You could also build your 2-D array manually with 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;
The code above assumes that you have at least one item in the finalData
, and that the length of all lists inside finalData
is the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With