Java Reference
In-Depth Information
You can think about the array as looking like the following:
0
0
0
0
42
0
0
Are you wondering why the value 42 is where it is? Can you guess why? The first bucket has an index number of
zero. In other words, the first bucket is referred to as myFirstArray[0]. So, myFirstArray[4] actually refers to the fifth
bucket (or position) within the array. To reiterate, an array with a length of seven has seven buckets with position/
index numbers ranging from zero to six. You can think of the array and its index numbers as looking like the following:
Index Number 0 1 2 3 4 5 6
0
0
0
0
42
0
0
Be careful, the index numbers are a source of countless errors!
Are you also wondering where all the zeros come from? The JVM inserts initial values when an array is created.
For numeric arrays, that value is zero (character arrays are set to the character 0 and referenced variable arrays are set
to null).
We are going to use an array to store our state values. Using an array will make adding or deleting states from the
stateDDM tag much easier. For example, if we did not use an array, the following code would be needed to define the
three state options in a drop-down menu:
<SELECT name="stateDDM">
<OPTION value="FL">FL</OPTION>
<OPTION value="GA">GA</OPTION>
<OPTION value="OK">OK</OPTION>
</SELECT>
If we wanted to add five states, we would have to add five start and end OPTION tags with the new abbreviations
specified in the body of the tags. Unfortunately, because the page designer can specify a state to be selected, it's even
more complicated. The tag handler needs to check the selected variable's value and generate the appropriate HTML
for that option to be selected. For example, if selected equals FL, the following HTML must be generated.
<OPTION value="FL" selected>FL</OPTION>
So, the tag handler needs several if statements like the following:
if (selected == "FL"){
<OPTION value="FL" selected>FL</OPTION>
} else {
<OPTION value="FL">FL</OPTION>}
if (selected == "GA"){
<OPTION value="GA" selected>GA</OPTION>
} else {
<OPTION value="GA">GA</OPTION>}
if (selected == "OK"){
<OPTION value="OK" selected>OK</OPTION>
} else {
<OPTION value="OK">OK</OPTION>}
 
Search WWH ::




Custom Search