C# How to convert Array[][] to Array[,]

Dear All,

I have a requirement where the data must be in the format of double[ , ].  In all my previous methods the data was in the format of double[][].

1- Aren't they the same thing
2- Any way of converting double[][] to double[ , ] ?

I have tried
double[,] data = dataArray;

Open in new window

It returns "cannot convert source double[][] to double[,]"

Who is Participating?
anarki_jimbelConnect With a Mentor Commented:
OK, I didn't want to invent a bicycle, and searched internet. If you have jugged array with the same number of elements in each "row" - there is a simple recipy:
Of course, there is no built-in procedure for that and you need to loop through outer and innere arrays.

More general procedure would work like below:
1. scan jugged array and find max value for both dimensions. In my first exanple it would be [2,5].

2. Declare and create a new array with these dimensions.

3. Copyarray"(a][b)" elements to [a,b]

4. Where j is less than max dimension - put 0s (I believe the array would be initiated with 0s, at least, in VB. Not 100% sure about C#.

Not, they are not same:

arrays like [][] are called also jugged arrays. I.e., they are "arrays of arrays"
If you declare something like:
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} }; you'll have:

2, 3, 4
5, 6, 7, 8, 9

In other words, it is a bit tricky to say what are dimensions for the array... And how can you convert this array into a truly 2-dimensional array?

Say, if we want an array like [2,5] we'll have

But if we want  [2,3] - we may need to throw away some array elements:

So, there is no strict recipy how to convert such arrays into each other. Obviously, convert [,] array to a jugged array much easier :)
A good view 4 understanding
All Courses

From novice to tech pro — start learning today.