简体   繁体   中英

Converting A Static “Level” Array To A Dynamic One And Still Get XYZ Coordinates

Okay, so currently I have a static world (can't be resized). I would like to know how I can convert this into an ArrayList instead of my current situation:

    for(int xP = 0; xP < WORLD_SIZE_X; xP++) {
        for(int zP = 0; zP < WORLD_SIZE_Z; zP++) {
            for(int yP = 0; yP < WORLD_SIZE_Y; yP++) {
            }  
        }  
    }

My question is, how can I make is so I can use

for(int i = 0; i < blocks.length; i++) {}

but still get the [x][y][z] coordinates?

Currently I'm using this to get the block:

blocks[x][y][z];

How would I get the XYZ with a dynamic ArrayList?

If you need more information, please tell me. I'll be glad to help you understand my situation.

ArrayList's equivalent of .length is .size() , and its equivalent of [...] is .get(...) .

That said, an ArrayList<ArrayList<ArrayList<...>>> is a pretty unwieldy structure to work with; and although it's theoretically "dynamic", in practice it's difficult to resize, because any resizing involves creating many new elements that you have to initialize.

If your goal is merely to be able to specify the size at runtime (but have the size remain constant after that), then you might find it easier to stick with arrays, but using variables worldSizeX and worldSizeY and worldSizeZ rather than constants.

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