Solved

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

Posted on 2011-09-07
4
206 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now