Link to home
Start Free TrialLog in
Avatar of AndyC1000
AndyC1000

asked on

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[,]"

Thanks
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Not, they are not same:
http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

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
2,3,4,0,0
5,6,7,8,9

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

So, there is no strict recipy how to convert such arrays into each other. Obviously, convert [,] array to a jugged array much easier :)
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A good view 4 understanding