Is there an one-liner (no looping) that converts List<double[]>
to double[,]
?

- 98,240
- 88
- 296
- 433

- 81,782
- 146
- 424
- 602
-
2That all depends how long you want your line to be :) (I suspect a few more than 80 chars). – leppie Aug 23 '10 at 09:00
-
@leppie, of course I want it as short as possible! – Graviton Aug 23 '10 at 09:00
-
@leppie: Wondering if 80 chars length was some standard? – abatishchev Aug 23 '10 at 09:24
-
@abatishchev: In the old days, it was, nowadays, I think people prefer 120. – leppie Aug 23 '10 at 09:26
-
@leppie: Never understood such small sizes. 19" monitors has wide spread occurrence. Many developers has monitors much larger that 19 - and 21", and 25". So 140-240 chars is easy to look over, imo – abatishchev Aug 23 '10 at 09:30
-
@abatishchev: In the old days text terminals were limited to 80 x 25 - 50 (vertical lines). – leppie Aug 23 '10 at 09:33
-
@leppie: Old days - 80x-early 90x - no problem to understand, technical limitation, etc. That's that. But follow such standards in 00x - is a nonsense imo. Specially when it turns into `throw new Exception( +
+ "some long " + – abatishchev Aug 23 '10 at 09:46+ "text");`
5 Answers
Converting to double[,]
can only be done by looping through the list and requires that all arrays contained in the list are of same size:
double[,] arr = new double[list.Count, list[0].Length];
for (int i = 0; i < list.Count; i++)
{
for (int j = 0; j < list[0].Length; j++)
{
arr[i, j] = list[i][j];
}
}
Of course, you can easily create a jagged double[][]
array of arrays by calling .ToArray()
:
double[] array = new double[] { 1.0, 2.0, 3.0 };
double[] array1 = new double[] { 4.0, 5.0, 6.0 };
List<double[]> list = new List<double[]>();
list.Add(array);
list.Add(array1);
double[][] jaggedArray = list.ToArray();

- 172,527
- 53
- 255
- 316
-
You also can add trailing zeroes if array isn't of the required size – abatishchev Aug 23 '10 at 09:23
-
@abatishchev: Yes, good point. This would require an additional pass to find the maximum length entry in the list first so you can determine the array size needed. – Dirk Vollmar Aug 23 '10 at 09:25
-
Finding the longest array in the list first is better to use list[0]. – Cheng Chen Aug 23 '10 at 09:39
-
@Danny: It's rather better to iterate one time more I think than trim all arrays longer than first – abatishchev Aug 23 '10 at 09:42
-
1
Well, you probably can't implement it without loops, but you can make the usage a one-liner :
double[,] array = list.To2DArray();
To2DArray
is an extension method implemented as follows:
public static class ExtensionMethods
{
public static T[,] To2DArray<T>(this IEnumerable<IEnumerable<T>> source)
{
var jaggedArray = source.Select(r => r.ToArray()).ToArray();
int rows = jaggedArray.GetLength(0);
int columns = jaggedArray.Max(r => r.Length);
var array = new T[rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < jaggedArray[i].Length; j++)
{
array[i, j] = jaggedArray[i][j];
}
}
return array;
}
}
Note that it will only work in C# 4, since earlier versions don't support covariance. This variant should work in C# 3 but it is more specific:
public static T[,] To2DArray<T>(this IEnumerable<T[]> source)
{
var jaggedArray = source.ToArray();
// same code from here
}

- 98,240
- 88
- 296
- 433

- 286,951
- 70
- 623
- 758
-
You could use `int rows = source.Count(); int columns = source.Max(r => r.Count());` directly without creating a jagged array first. – Dirk Vollmar Aug 23 '10 at 09:57
-
Yes, but I need the jagged array afterwards to access the data (I could use the ElementAt method but it's wouldn't work for enumerables that can be enumerated only once) – Thomas Levesque Aug 23 '10 at 11:14
This syntax should work: return new List{new double[] { minX, minY }, new double[] { maxX, maxY }};

- 992
- 10
- 13
If a 2 dim array is to be created from List of 1 dim array then looping is required, though it may not look like that at the call-site.

- 28,432
- 15
- 72
- 133
public static T[,] ToMultidimensional<T>(this T[][] arr, int maxSize)
{
T[,] md = (T[,])Array.CreateInstance(typeof(double), arr.Length, maxSize);
for (int i = 0; i < arr.Length; i++)
for (int j = 0; j < arr[i].Length; j++)
md[i, j] = arr[i][j];
return md;
}
var arr = new List<double[]>
{
new double[] { 1, 2, 3 },
new double[] { 4, 5 }
}
.ToArray();
var j = arr.ToMultidimensional(arr.Max(a => a.Length));

- 98,240
- 88
- 296
- 433