# How to convert a Double[][] to a Double[*,*]

Hi All,

The best I've managed so far is something like

List<double[]> something;
{
}

but I cant seem to then change my double[][] to a double[*,*] which is required by a control I am using,

Thanks,

UKWebGuy
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Well, with double[][] the rows can be with different length, for example row1 have 2 items, row2 5 items, row3 100 items etc... while with double[*,*] each row contains the exact number of items that were predefined.

Therefore, Casting from double[][] to double[*,*] cannot be made on some situations. This is probably the reason why you can do the casting either implicitly of explicitly.

I have no idea if you for each dr.read you add the exact number of doubles to that row. If not then you will need to at least know the length of the longest row.

List<double[]> something = new List<double[]>();
int maxLen = 0;
for (int i = 0; i< 5; i++)
{
something.Add(new double[] { 1.0, 2.0 });
maxLen = Math.Max(maxLen, something[something.Count - 1].Length);
}

double[,] doubleArray = new double[something.Count, maxLen];

for (int i = 0; i < something.Count; i++)
{
for (int j = 0; j < maxLen; j++)
{
doubleArray[i, j] = something[i][j];
}
}

If your double[][] doesn't have a rows with the same length then "empty" cells will have the value of 0.0
0
Author Commented:
Thanks,

I am only ever adding 2 rows to the array, so each one has the same length.

Is there any way to do it without looping through each record twice?

Thanks,

UkWebGuy
0
Commented:
Are you saying that your array is double[2,2]?
0
Author Commented:
Yeah, I am only ever adding 2 items into the array, so it is [x,2]
0
Commented:
Well, you said that you only add 2 rows... you are actualy have x rows and 2 columns.
If you can get from the Data Reader the number of rows that it has (Maybe RowsAffected, but I doubt that it will give a usuable number), then you can create the double[*,*] array and use it instead of using a list.

I'm not familiar with Data Reader that much in order to know if it has a property that can give you the number of rows.

Maybe you can use DataAdapter instead, and fill a DataSet with the recordset...
0
Author Commented:
The problem I have is that I dont know how many 2 item arrays I am going to add,

it could be [5,2] or [15,2] or [100,2]...

Regards,
0
Author Commented:
Hmm, so the only way of doing it is to know what I am going to put in it beforehand. I'll give it a try and see what I can do when I get a chance.

Sorry about my above reply, I didnt see that you replied before I posted.

Thanks,
0
Commented:
Actualy, right now I'm thinking of another solution... Let me check if it works fine and if it does then I'll post it here.
0
Commented:
Ok, this should work:

List<double> something = new List<double>();
for (int i = 0; i < 30; i++)
{
}

double[,] doubleArray = new double[something.Count / 2, 2];
double[] src = something.ToArray();

Buffer.BlockCopy(src, 0, doubleArray, 0, 8 * doubleArray.Length);

I'm using a list of double instead of list of double[] and I'm adding the 2 items on each iteration istead of 1 (array of 2 items).

Maybe it can also be done with List<double[]> but ot gave me an exception when trying to do a Buffer.BlockCopy and I didn't want to waste too much time on it.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Ohh, a quick note before you will ask... BlockCopy gets the last parameter as number of bytes to copy... so because each double takes 8 bytes then I need to copy 8 * doubleArray.Length bytes.
0
Author Commented:
Great, Thanks, Ill try that when I get a chance and let you know!

Regards,
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.