Chapter 11

Using Arrays to Juggle Values

IN THIS CHAPTER

check Dealing with several values at once

check Searching for things

check Creating values as you get a program running

Welcome to the Java Motel! No haughty bellhops, no overpriced room service, none of the usual silly puns. Just a clean double room that’s a darn good value!

Getting Your Ducks All in a Row

The Java Motel, with its ten comfortable rooms, sits in a quiet place off the main highway. Aside from a small, separate office, the motel is just one long row of ground-floor rooms. Each room is easily accessible from the spacious front parking lot.

Oddly enough, the motel’s rooms are numbered 0 through 9. I could say that the numbering is a fluke — something to do with the builder’s original design plan. But the truth is that starting with 0 makes the examples in this chapter easier to write.

Anyway, you’re trying to keep track of the number of guests in each room. Because you have ten rooms, you may think about declaring ten variables:

int guestsInRoomNum0, guestsInRoomNum1, guestsInRoomNum2, guestsInRoomNum3, guestsInRoomNum4, guestsInRoomNum5, guestsInRoomNum6, guestsInRoomNum7, guestsInRoomNum8, guestsInRoomNum9;

Doing it this ...

Get Java For Dummies, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.