6

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 :(
}
Community
  • 1
  • 1
Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • 3
    A `List` can only possibly be converted to a `double[,]` if the arrays are all the same length. It could be converted to a `double[][]` easily (that's what `ToArray` returns). If this isn't clear to you, compare http://msdn.microsoft.com/en-us/library/2yd9wwz4 and http://msdn.microsoft.com/en-us/library/2s05feca – Tim S. Jul 02 '12 at 14:35
  • Your method signature indicate a 2D dimensional array as return, while you are returning a single dimensional array. In fact, you are returning an array of array. – Steve B Jul 02 '12 at 14:35
  • what if your return value is `double[][]` – Jodrell Jul 02 '12 at 14:36
  • "No. In fact, these aren't necessarily compatible arrays" http://stackoverflow.com/a/678211/284240 – Tim Schmelter Jul 02 '12 at 14:36
  • This not the first time I've seen this question on SO. – Jodrell Jul 02 '12 at 14:39
  • Why not return an `IList>`? – Jodrell Jul 02 '12 at 14:44
  • Return should be `double[,]` since its needed buy another method somewhere else – Dumbo Jul 02 '12 at 14:49

3 Answers3

5

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

finalData.ToArray() will produce double[][] because:

  • List<T>.ToArray() returns T[]
  • List<T[]>.ToArray() returns T[][]
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Isn't this contradicting with the `exactly opposite statement` here [first line] - http://stackoverflow.com/a/11295805/763026 ? – Angshuman Agarwal Jul 02 '12 at 14:48
  • @AngshumanAgarwal: "Since ToArray returns a one-dimensional array" - yes, it's correct and equals to my statement. List.ToArray() returns T[]. List.ToArray() returns T[][]. – abatishchev Jul 02 '12 at 14:51
1
  1. Instantiate a new double array with the largest size [length of the list, largest array length in the list]
  2. Walk through the list with double for cycle (first on the list, the nested on the current list item) and fill the new array
Peter Kiss
  • 9,309
  • 2
  • 23
  • 38