15 Lookup Arrays
Even beginning programmers quickly realize that arrays make it very easy to perform the same operation(s) on each element of a homogeneous group of elements. However, what they often don’t realize is that arrays can be used in less obvious ways as well. Lookup arrays are one example.
Motivation
Highway exits used to be numbered using consecutive integers. The first (on a particular highway) was exit 1, the second was exit 2, etc. Later, highway exit numbers were changed to correspond (at least closely) to the mile marker (i.e., the number of miles since the start of the highway). So, the exit at mile marker 1 is numbered 1, the exit at mile marker 15 is numbered 15, etc. The problem then arises of how to “convert” an old exit number to a new exit number.
Review
As you know, arrays have two important characteristics. First, each element must be of the same type (i.e., the elements must be homogeneous). Second, the indexes are consecutive, non-negative int
values. However, beyond that, there are no restrictions on how they can be used.
When you are first introduced to arrays, the examples all tend to involve “data processing” of some kind. For example, they involve weekly sales, annual populations, grades on exams, etc., and the indexes are just used to differentiate the elements. However, the values of the indexes can be meaningful in their own right. For example, in the current context, the indexes could represent exit numbers.
Thinking About The Problem
All that remains to think about is whether the indexes should represent the old exit numbers or the new exit numbers. Fortunately, given the nature of the old and new exit numbers, the correct representational scheme is obvious. Except for the fact that array indexes start at 0, they seem to have the same properties as the old highway numbering system. So, if there are five exits, you can use an array of length six (with indexes of 0
, 1
, …, 5
) to hold information about each exit. In this case, the information that you want to associate with each old exit number is the new exit number, which is, itself, an int
. So, you can keep the information you need in an int[]
of length six.
For example, if the new exit numbers are at mile markers 1, 15, 16, 28, and 35, you can store them in the following static
array named NEW_NUMBERS
:
private static final int[] NEW_NUMBERS = {-1, 1, 15, 16, 28, 35};
where the first element is -1
to indicate that there is no old exit number 0. This is illustrated in Figure 15.1.
Then, the new exit number corresponding to old exit number i
is just NEW_NUMBERS[i]
. For example, NEW_NUMBERS[3]
is the new exit number that corresponds to old exit number 3
.
The Pattern
The pattern follows immediately from this example:
- Create an array in which the indexes correspond to the key that will be used to perform the look-up, and the elements correspond to the value that is to be determined.
- Create a method that validates the key and returns the appropriate eleme