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

                398 Chapter 10 Arrays and ArrayLists
2-element array and each of the two elements is a reference to its own 3-element array of ints. This picture
illustrates what were talking about:
normal way to think of x
actual x
    x[0] x[1]
x.length is 2
x[0].length is 3 x[1].length is 3
       Since x is actually a reference to a 2-element array, x.length holds the value 2. Or thinking about x in the “normal” way (above left picture), x.length holds the number of rows in x. As you can see above, x[0] is a reference to a 3-element array. Thus, x[0].length holds the value 3. Or thinking about x in the “normal”way(aboveleftpicture),x[0].length holdsthenumberofcolumnsinx.Thepointofallthis is that the length property can be used for iterating through the elements in a two-dimensional array. In Figure 10.17, note how the first loop uses x.length to iterate through each row in x, and note how the secondloopusesx[0].length toiteratethrougheachcolumninx.
Example
Let’s put these two-dimensional array concepts into practice by using a two-dimensional array in the con-
Apago PDF Enhancer
text of a complete program. The program, built for a Kansas and Missouri airline company, tells customers when airplanes are expected to arrive at various Kansas and Missouri airports. It uses a two-dimensional array to store flight times between cities, and it displays output like this:
  Wch
Top
KC
Col
StL
Wch
0
23
31
44
59
Top
22
KC Col
30 42
14 25
0 11
12 0
30 14
StL
55
37
28
12
0
  27
41
0
9
Different rows correspond to different cities of origin. Different columns correspond to different cities of destination. The labels are abbreviations for city names: “Wch” stands for Wichita, Kansas. “Top” stands for Topeka, Kansas. “KC” stands for Kansas City, Missouri. “Col” stands for Columbia, Missouri. “StL” stands for St. Louis, Missouri. Thus, for example, it takes 25 minutes to fly from Topeka to Columbia. How long does it take to go the other way, from Columbia to Topeka? 27 minutes. Columbia to Topeka takes longer because the trip goes east to west, and airplanes have to contend with head winds from North America’s west-to-east jet stream.
Let’s analyze the program by starting with Figure 10.18’s FlightTimesDriver class. Note how the main method declares and creates a flightTimes table with a two-dimensional array initializer. And note how the initializer puts each table row on a line by itself. That’s not required by the compiler, but it makes for elegant, self-documenting code. It is self-documenting because readers can easily iden- tify each row of table data by looking at a single row of code. After initializing the flightTimes table, main initializes a one-dimensional array of city names and then calls the FlightTimes constructor, the displayFlightTimesTable method, and the promptForFlightTime method. We’ll discuss the constructor and those two methods next.
It takes 25 minutes to fly from Topeka to Columbia.






















































   430   431   432   433   434