Qinusty
Qinusty

Reputation: 339

How to use a 3 Dimensional Arrays and Loop through the Width,Height, Length?

I am Programming a Small Tile System for a game I'm working on and ran into an issue with Arrays, I can't figure out how to loop through.

public Tile[,,] TileList = new Tile[50, 50, 2];

Usually in One dimensional arrays I use array.length or array.count, but neither of these would work here. Any suggestions would be greatly appreciated.

Upvotes: 0

Views: 165

Answers (3)

Rudra Sisodia
Rudra Sisodia

Reputation: 133

for(int width=0;width<array.GethLenght(0);width++)
   for(int height=0;height<array.GethLenght(1);height++)
        for(int lenght=0;lenght<array.GethLenght(2);lenght++)
              // array[width,height,width]

Try this

Upvotes: 1

Henrik
Henrik

Reputation: 23324

for (int i = 0; i < TileList.GetLength(0); ++i)
    for (int j = 0; j < TileList.GetLength(1); ++j)
        for (int k = 0; k < TileList.GetLength(2); ++k)
           // do something with TileList[i,j,k];

But note that jagged arrays TileList[][][] are usually preferred, as they are more efficient.

Upvotes: 2

heltonbiker
heltonbiker

Reputation: 27575

Use

array.GetLength(dim);

This will give you the length along given dimension.

Upvotes: 2

Related Questions