简体   繁体   中英

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.

Use

array.GetLength(dim);

This will give you the length along given dimension.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM