• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1menyo  Members

Posted 12 April 2014 - 04:46 AM

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[i] = 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, 12 April 2014 - 04:56 AM.

Latest finished project Project

### #2Aurioch  Members

Posted 12 April 2014 - 05:08 AM

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, 12 April 2014 - 09:04 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.