Look-up Arrays
A Programming Pattern
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
An Example: Highway Exit Numbers (cont.)
javaexamples/programmingpatterns/LookupArrays.java
(Fragment: newExitNumberFor)
An Example: Highway Exit Names (cont.)
javaexamples/programmingpatterns/LookupArrays.java
(Fragment: exitNameFor)
From a Key to an Index
- Large Contiguous Keys:
- The keys are contiguous but don't start at 0 in which case
you can subtract the key from the smallest possible key
(e.g., if you have annual data starting in 2015 then
index = year - 2015;
- An Example:
-
javaexamples/programmingpatterns/LookupArrays.java
(Fragment: sales)
From a Key to an Index (cont.)
- Non-Contiguous, Regular Keys:
- An Example:
-
javaexamples/programmingpatterns/LookupArrays.java
(Fragment: letterGrade)
From a Key to an Index (cont.)
Pass/Fail Grading
javaexamples/programmingpatterns/LookupArrays.java
(Fragment: passFail)
An Example: Population (cont.)
javaexamples/programmingpatterns/LookupArrays.java
(Fragment: population)
There's Always More to Learn