How to convert list of arrays into a multidimensional array

I need to convert the following collection into double[,]:

 var ret = new List<double[]>();

All the arrays in the list have the same length. The simplest approach, ret.ToArray(), produces double[][], which is not what I want. Of course, I can create a new array manually, and copy numbers over in a loop, but is there a more elegant way?

Edit: my library is invoked from a different language, Mathematica, which has not been developed in .Net. I don't think that language can utilize jagged arrays. I do have to return a multidimensional array.


Solution 1:

I don't believe there's anything built into the framework to do this - even Array.Copy fails in this case. However, it's easy to write the code to do it by looping:

using System;
using System.Collections.Generic;

class Test
{
    static void Main()
    {
        List<int[]> list = new List<int[]>
        {
            new[] { 1, 2, 3 },
            new[] { 4, 5, 6 },
        };

        int[,] array = CreateRectangularArray(list);
        foreach (int x in array)
        {
            Console.WriteLine(x); // 1, 2, 3, 4, 5, 6
        }
        Console.WriteLine(array[1, 2]); // 6
    }

    static T[,] CreateRectangularArray<T>(IList<T[]> arrays)
    {
        // TODO: Validation and special-casing for arrays.Count == 0
        int minorLength = arrays[0].Length;
        T[,] ret = new T[arrays.Count, minorLength];
        for (int i = 0; i < arrays.Count; i++)
        {
            var array = arrays[i];
            if (array.Length != minorLength)
            {
                throw new ArgumentException
                    ("All arrays must be the same length");
            }
            for (int j = 0; j < minorLength; j++)
            {
                ret[i, j] = array[j];
            }
        }
        return ret;
    }

}

Solution 2:

You can do following as extension:

    /// <summary>
    /// Conerts source to 2D array.
    /// </summary>
    /// <typeparam name="T">
    /// The type of item that must exist in the source.
    /// </typeparam>
    /// <param name="source">
    /// The source to convert.
    /// </param>
    /// <exception cref="ArgumentNullException">
    /// Thrown if source is null.
    /// </exception>
    /// <returns>
    /// The 2D array of source items.
    /// </returns>
    public static T[,] To2DArray<T>(this IList<IList<T>> source)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }

        int max = source.Select(l => l).Max(l => l.Count());

        var result = new T[source.Count, max];

        for (int i = 0; i < source.Count; i++)
        {
            for (int j = 0; j < source[i].Count(); j++)
            {
                result[i, j] = source[i][j];
            }
        }

        return result;
    }

Solution 3:

There's no easy way to do this because in the situation you're describing, there's nothing stopping the double[] arrays in the list from being different sizes, which would be incompatible with a two-dimensional rectangular array. However, if you are in the position to guarantee the double[] arrays all have the same dimensionality, you can construct your two-dimensional array as follows:

var arr = new double[ret.Count(),ret[0].Count()];

for( int i=0; i<ret.Count(); i++ ) {
  for( int j=0; j<ret[i].Count(); j++ )
    arr[i,j] = ret[i][j];
}

This will produce a run-time error if any of the double[] arrays in the list are shorter than the first one, and you will lose data if any of the arrays are bigger than the first one.

If you are really determined to store a jagged array in a rectangular array, you can use a "magic" value to indicate there is no value in that position. For example:

var arr = new double[ret.Count(),ret.Max(x=>x.Count())];

for( int i=0; i<ret.Count(); i++ ) {
  for( int j=0; j<arr.GetLength(1); j++ )
    arr[i,j] = j<ret[i].Count() ? ret[i][j] : Double.NaN;
}

On an editorial note, I think this is a Very Bad Idea™; when you go to use the rectangular array, you have to check for Double.NaN all the time. Furthermore, what if you wanted to use Double.NaN as a legitimate value in the array? If you have a jagged array, you should just leave it as a jagged array.