Solved

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

Posted on 2011-09-07
4
209 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:AndyC1000
  • 4
4 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 36500710
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 :)
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 36500731
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:
http://bytes.com/topic/c-sharp/answers/590928-convert-jagged-array-into-multidimensional-array
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#.

Done
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 36500738
A good view 4 understanding
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 36500739
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Video Player 2017 5 35
converto datetime always changes format 9 41
Problem!!! 8 37
Sort GridView by ID Descending 1 17
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question