Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast way of iterate over trues in boolean array

I have a big array of booleans and i need just to work with the true elements, and need theyer index.

for (int x = 0; x < cells.length; x++) {
        for (int y = 0; y < cells[0].length; y++) {

                if (cells[x][y]) {
                    g.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
                }

        }
    }

so how can i do that without the two foor loops and save the time?

like image 587
TheSorm Avatar asked Dec 14 '25 22:12

TheSorm


1 Answers

I'd say this is the best you can get.

You are already using a primitive array and I expect primitive boolean type.

The only thing I could suggest is saving cells.length and cells[0].length in final variables before the loops, it could save some time, but the compiler may already optimize this on its own.

like image 176
Aleksandr Avatar answered Dec 16 '25 12:12

Aleksandr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!