Page 436 - Introduction to Programming with Java: A Problem Solving Approach
P. 436

                402 Chapter 10 Arrays and ArrayLists
Figure 10.19b’s displayFlightTimesTable method displays the flight times table. In doing so, it employs an interesting formatting technique. First look at the two local named constants, which are sepa- rately defined format strings. You have been using literal format strings embedded in strings of text for some time now in the arguments of printf method calls. But instead of embedding literal format strings, some- times it’s easier to understand if you declare them separately as named constants. If you go back and count the spaces in the six-column table of flight times, you’ll see that each column is exactly5 spaces wide. So the labels at the top of the columns and the numbers in the columns must both be formatted to use exactly 5 spaces. Thus, the format string for the labels (CITY_FMT_STR) should be "%5s", and the format string for the integer entries (TIME_FMT_STR) should be "%5d". Using named constants for format strings al- lows each format string to be used in many places, and it makes it easy and safe to alter them at any later time—just change the values assigned to the named constants at the beginning of the method.
In the displayFlightTimesTable method, note the three for loop headers. They all use the length property for their termination condition. Since length holds 5, the program would run correctly if you replaced the length termination conditions with hardcoded 5’s. But don’t do it. Using the length prop- erty makes the implementation more scalable. Scalable means it’s easy to change the amount of data that the program uses. For example, in the FlightTimes program, using a cities.length loop termination con- dition means that if you change the number of cities in the program, the program will still work properly.
Multi-Dimensional Arrays
Arrays may have more than two dimensions. Arrays with three or more dimensions use the same basic
syntax except they have additional []’s. The first pair of brackets corresponds to the largest scale, and
each subsequent pair of brackets nests within the previous pair, at progressively smaller levels of scale. For
private int[][][] flightTimes;
10.10 Arrays of Objects
You learned in the previous section that a two-dimensional array is actually an array of references where each reference points to an array object. Now let’s look at a related scenario. Let’s look at an array of refer- ences where each reference points to a programmer-defined object. For example, suppose you’d like to store total sales for each sales clerk in a department store. If sales clerk Amanda sells two items for $55.45 and $22.01, then you’d like to store 77.46 for her total-sales value. You can store the sales clerk data in an array, clerks, where each element holds a reference to a SalesClerk object. Each SalesClerk object holds a sales clerk’s name and the total sales for that sales clerk. See Figure 10.20 for an illustration of what we’re talking about.
The clerks array is an array of references. But most folks in industry would refer to it as an array of objects, and that’s what we’ll do as well. An array of objects isn’t that much different from an array of primi- tives. In both cases, you access each array element with square brackets (e.g., clerks[0], clerks[1]). But there are some differences that you should be aware of, and those differences are the main focus of this section.
Apago PDF Enhancer
example, suppose the Missouri-Kansas airline company decides to go “green” and expands its fleet with new solar-powered airplanes and wind-powered airplanes that burn hydrogen. The new airplanes have dif- ferent flight times than the original jet-fuel airplanes. Thus, they need their own flight-times tables. The solution is to create a three-dimensional array where the first dimension specifies the airplane type—0 for the jet-fuel airplanes, 1 for the solar-powered airplanes, and 2 for the wind-powered airplanes. Here’s how to declare the new three-dimensional flightTimes array instance variable:
 





















































































   434   435   436   437   438