Solved

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

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

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 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