AppSensei
AppSensei

Reputation: 8400

Initialize a boolean array element to be true?

How to initialize a single element to be true rather than initializing the whole array.

    do {
        if (flightClass == 1) {
            Arrays.fill(seats, true);
        } else if (flightClass == 2) {
            Arrays.fill(seats, true);
        }
    } while (i <= 10);

My approach was to do this....

    do {
        if (flightClass == 1) {
            int enterSeat = input.nextInt();
            Arrays.fill(seats[enterSeat], true);
        } else if (flightClass == 2) {
            Arrays.fill(seats, true);
        }
    } while (i <= 10);

}

Upvotes: 0

Views: 4232

Answers (2)

JohnB
JohnB

Reputation: 13713

You can simply write

seats[enterSeat] = true

to set a single array element.

Comment: It is, however, strange to put everything into the while loop, and it is even more strange not to change i inside the loop. Are you sure your logic is correct?

Upvotes: 3

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 235984

Instead of this:

Arrays.fill(seats[enterSeat], true);

Simply do this:

seats[enterSeat] = true;

Upvotes: 5

Related Questions