Advertisement

Combine a 2D array with a 3D array?

Started by April 12, 2014 10:46 AM
0 comments, last by Josip Mati? 10 years, 9 months ago

Hi,

I am looking for a way to combine a 2D array with a 3D array.

I have a function that returns a float[,] it gets populated with the width and height of my map. Now i am trying to do something like this:

float[,,] 3dArray = new float[amount, width, height];

for (int i = 0; i < amount; i++)

{

3dArray = MethodReturningFloatArray[,];

}

So basically i want to populate the 2nd and 3th dimension of my 3D array with a 2D array generated by a method (float[,]).

I don't know if my code is 100% correct, this I'm writing completely from head smile.png

Function for populating 2nd and 3rd dimension should go like this:


void PopulateJKDimensions(ref float[,,] 3dArray, int i, float[,] 2dArray)
{
    for (int j = 0; j < 2dArray.GetLength(0); j++)
        for (int k = 0; k < 2dArray.GetLength(1); k++)
            3dArray[i, j, k] = 2dArray[j, k];
}

What's important to say here is that appropriate dimensions have to be of same sizes, otherwise you need to add additional code to take care of it.

ref is to ensure that changes made inside function to 3dArray remain visible outside the function. I'm still not 100% sure if it would be the same without ref keyword.

And then for main code:


// ...

float[,,] 3dArray = new float[amount, width, height];
float[,] 2dArray;

for (int i = 0; i < amount; i++)
{
    2dArray = MethodReturning2dFloatArray( );
    PopulateJKDimensions(ref 3dArray, i, 2dArray);
}

// ...

EDIT: Or even easier:


// ...

float[,,] 3dArray = new float[amount, width, height];
float[,] 2dArray;

for (int i = 0; i < amount; i++)
{
    2dArray = MethodReturning2dFloatArray( );

    for (int j = 0; j < width; j++)
        for (int k = 0; k < height; k++)
            3dArray[i, j, k] = 2dArray[j, k];
}

// ...

This topic is closed to new replies.

Advertisement