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
source share
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

finalData.ToArray() double[][], :

  • List<T>.ToArray() T[]
  • List<T[].ToArray() T[][]
+1
  • Create a new double array with the largest size [list length, longest array length in the list]
  • Go through the list with a double loop (first in the list nested in the current element of the list) and fill in a new array
+1
source

All Articles