Solved

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

Posted on 2011-09-07
4
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
4 Comments
 
LVL 30

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 30

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 30

Expert Comment

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

Expert Comment

by:anarki_jimbel
ID: 36500739
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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