Quick way to add 1D array to Multidimensional array

Hi GURUs,

I am trying to find a quick way to add 1 dimensional array into multidimensional array. For example:
object[] oneDArray1 = new object[10];
object[] oneDArray2 = new object[10];
object[] oneDArray3 = new object[10];Then, I need to add this array into 2D Multidimensional array.
object[,] twoDArray = new object[3, 10];
I tried to do this:
twoDArray[0] = oneDArray1; //of course it won't work because twoDArray isn't Jagged Array

Currently, I set the array one by one, but it is time consuming when I have huge number of arrays. And no, we can't use jagged array (eg. double[][] twoDArray).

Thanks Guru for any help.

Cheers :)
[710 byte] By [ryu] at [2007-11-20 9:10:16]
# 1 Re: Quick way to add 1D array to Multidimensional array
object[,] twoDArray = new object[3, 10];
twoDArray[0] = oneDArray1; //of course it won't work because twoDArray isn't Jagged Array

But twoDArray[0,0] would work. What you've declared is a rectangular array. i.e. it has rows and columns. You need to specify both the row and column you want to store your oneDArray in.
Mutant_Fruit at 2007-11-9 11:34:02 >
# 2 Re: Quick way to add 1D array to Multidimensional array
Hi Mutant_Fruit,

You mean:
for(int i=0; i<10; i++)
{
twoDArray[0, i] = oneDArray1[i];
twoDArray[1, i] = oneDArray2[i];
twoDArray[2, i] = oneDArray3[i];
}That will work fine. But the problem with it is that if you have 10000 column in oneDArray1 and you need to set a lot of them, it will be really slow.

What I want is to be able to add the oneDArray directly to twoDArray. Eg.twoDArray[0] = oneDArray1;Or use some sort of memcpy.

Please note:
I can't use Buffer.BlockCopy() because the array can be string.

Thanks again for any help.

Cheers :)
ryu at 2007-11-9 11:35:04 >
# 3 Re: Quick way to add 1D array to Multidimensional array
I'm confused as to waht you want. Why don't you just use 2D arrays from the start? Copying from one array to another is going to be slow if you're doing it 1000's of times a second no matter what language you're in. So avoid the copying altogether, or at least give me a better description of waht you want.
Mutant_Fruit at 2007-11-9 11:36:03 >
# 4 Re: Quick way to add 1D array to Multidimensional array
Hi Mutant_Fruit,

Well, I want to get the value of the array into Multidimensional array. Look at this example for Jagged array
object[] oneDArray1 = new object[10];
object[] oneDArray2 = new object[10];
object[] oneDArray3 = new object[10];
for (int i = 0; i < 10; i++)
{
oneDArray1[i] = i * 10;
oneDArray2[i] = i * 10;
oneDArray3[i] = i * 10;
}

object[][] twoDArray = new object[3][];
twoDArray[0] = oneDArray1;
twoDArray[1] = oneDArray2;
twoDArray[2] = oneDArray3;

//now, twoDArray[0][0] = oneDArray1[0] which is 0.

oneDArray1[0] = 123; //now twoDArray[0][0] = 123
As you can see, this method is very fast because it only re-assigning the pointer. I need to do it somehow in Multidimensional array.

The reason I post this message is because I have jagged array and I need to convert it to Multidimensional array. This is because Excel Range.Value2 is only accepting Multidimensional array.

Thanks for your help.

Cheers :)
ryu at 2007-11-9 11:36:58 >
# 5 Re: Quick way to add 1D array to Multidimensional array
hmm, you could try using dictionaries... they seem flexible enough to do what you want...

simply use an integer as a key and a multidimensional array as the value...

so it might be something like this:


Dictionary<int , object[,,,]> myDictionary;


is this along the lines of what you are talking about?

regards
adam
Anti-Rich at 2007-11-9 11:37:58 >
# 6 Re: Quick way to add 1D array to Multidimensional array
Hi Anti-Rich,

Thanks for replying.

Sorry that I wasn't clear to describe my problem. I actually have a DataSet and I need to convert this DataSet to Excel Application. Currently I can dooRng.Value2 = oDataTable.Rows[iRowIdx].ItemArrayThis code work if the Range is within single row. Now, if you have 1000 rows, this is process is very slow. It takes about 20 seconds to loop through the range and set the rows.

Alternatively, I could set the value of the range at one go. So says that if you want to set from Row #1 Column #1 to Row #1000 Column #1000, you could doobject[,] 2DMultiDimensionalArray = new object[1000, 1000];
oRng = oSheet.get_Range("A1", "ALL1000");
oRng.Value2 = 2DMultiDimensionalArray;Can you see my dilemma? In order to set the excel value at one go, I MUST convert each DataRow.ItemArray into MultiDimensional array. So, no, I can't use any other method. I need to add Single Dimensional Array into MultiDimensional array (NOT jagged array because I have try it and it throw an error).An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in mscorlib.dll

Additional information: Specified array was not of the expected type.
Any help from GURUs is greatly appreciated!

Cheers :)
ryu at 2007-11-9 11:39:08 >