Problem with arrays

X

Xarky

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
 
S

ShaneB

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;
}
}

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

CopyToArray(myMethod1(), myArray, 0);

ShaneB
 
T

Thomas P. Skinner [MVP]

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

Thomas P. Skinner [MVP]


ShaneB said:
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;
}
}

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

CopyToArray(myMethod1(), myArray, 0);

ShaneB

Xarky said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top