Public Group

# Combine a 2D array with a 3D array?

This topic is 2014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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[,]).

Edited by menyo

##### Share on other sites

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

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

// ...

Edited by Aurioch

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 23
• 42
• 75