Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with arrays

Xarky
Guest
 
Posts: n/a
#1: Nov 16 '05
Hi,
I have a two dimensional array as follows:

double[,] myArray = new double[14, 41];

Now I have a method which is returning double[], which is of size 41.

I need to put the data of the array returned into myArray.


myArray[0, ???] = myMethod1(); // return double[41]
myArray[1, ???] = myMethod2(); // return double[41]
etc

I hope someone understands my problem and can help me out.


Thanks in Advance

ShaneB
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Problem with arrays


You're going to have to copy them in a loop. I threw together this function
which should make it a simple task.


public void CopyToArray(double[] sourceArray, double[,] destinationArray,
int destinationArrayIndex)
{
Debug.Assert(sourceArray.Length == destinationArray.GetLength(1)); //
make sure the to/from arrays are the same length

for (int i=0; i<sourceArray.Length; i++)
{
destinationArray[destinationArrayIndex, i] = sourceArray[i];
}
}

And in your case, you would call it like this:

CopyToArray(myMethod1(), myArray, 0);

ShaneB

"Xarky" <bernardpace@yahoo.com> wrote in message
news:bc42e1a.0411061111.4658017e@posting.google.co m...[color=blue]
> Hi,
> I have a two dimensional array as follows:
>
> double[,] myArray = new double[14, 41];
>
> Now I have a method which is returning double[], which is of size 41.
>
> I need to put the data of the array returned into myArray.
>
>
> myArray[0, ???] = myMethod1(); // return double[41]
> myArray[1, ???] = myMethod2(); // return double[41]
> etc
>
> I hope someone understands my problem and can help me out.
>
>
> Thanks in Advance[/color]


Thomas P. Skinner [MVP]
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Problem with arrays


You can use jagged arrays. For example double [][] myArray; You can then
assign like this: myArray[i]=myMethod(); Assumes myMethod returns double[].
Basically jagged arrays are arrays of arrays.

Thomas P. Skinner [MVP]


"ShaneB" <stormfire1@yahoo.com> wrote in message
news:uRP8ZpDxEHA.2836@TK2MSFTNGP11.phx.gbl...[color=blue]
> You're going to have to copy them in a loop. I threw together this
> function which should make it a simple task.
>
>
> public void CopyToArray(double[] sourceArray, double[,] destinationArray,
> int destinationArrayIndex)
> {
> Debug.Assert(sourceArray.Length == destinationArray.GetLength(1));
> // make sure the to/from arrays are the same length
>
> for (int i=0; i<sourceArray.Length; i++)
> {
> destinationArray[destinationArrayIndex, i] = sourceArray[i];
> }
> }
>
> And in your case, you would call it like this:
>
> CopyToArray(myMethod1(), myArray, 0);
>
> ShaneB
>
> "Xarky" <bernardpace@yahoo.com> wrote in message
> news:bc42e1a.0411061111.4658017e@posting.google.co m...[color=green]
>> Hi,
>> I have a two dimensional array as follows:
>>
>> double[,] myArray = new double[14, 41];
>>
>> Now I have a method which is returning double[], which is of size 41.
>>
>> I need to put the data of the array returned into myArray.
>>
>>
>> myArray[0, ???] = myMethod1(); // return double[41]
>> myArray[1, ???] = myMethod2(); // return double[41]
>> etc
>>
>> I hope someone understands my problem and can help me out.
>>
>>
>> Thanks in Advance[/color]
>
>[/color]


Closed Thread