Java Reference
In-Depth Information
a[3] = 8;
a[4] = 10;
Again, note that counting starts at 0. The reason is that Java will search for the element
a[k] at address a + k *4 (assuming that the array contains four-byte elements).
It seems tedious to use six lines of code to define and populate an array of five elements.
Fortunately, in Java these lines can be merged into a single line as shown below.
int [] a =
{
2,4,6,8,10
}
;
The above line is a shorthand that performs three operations: allocates twenty consecutive
bytes to store five integers, makes the variable a equal to the address of the allocated
memory, and finally populates the array with the numbers
{
2,4,6,8,10
}
.
. Of course, we can
go ahead and explicitly write all 500 numbers that initially populate the array. However,
there is a more compact way to perform the initialization. In particular, note that arrays
and loops go together like ham and eggs (or peanut butter and jelly if you happen to be
vegetarian). If you go to a play and see a gun in the props, odds are that it will be fired
by act three. Similarly, if you see an array declaration, odds are that there will be a loop
that traverses through its elements. The most popular and easy way to traverse through the
elements of an array is using a for loop, although using a while loop can also be warranted
in some rear cases. An array with elements:
Next, consider an array that contains the numbers:
{
2,4,6,8, ... ,1000
}
{
2,4,6,8, ... ,1000
}
can be populated in Java
using the following code segment.
int [] a = new i n t [500];
for ( int i=2, j=0; i < =1000;
j++) {
i+=2,
a[j] = i;
}
The code performs the operation a[j] = i multiple times. The value of i starts at 2
and goes up to 1000 in increments of 2. The value of j starts at 0 and goes up to 499 in
increments of 1. We therefore have two conditions: i < = 1000 and j < = 499. Note that we
only need to include one of the two conditions in the second argument of the for statement
because when i becomes 1000, j will become equal to 499. Note as well that, while the first
and third part of a for statement can contain multiple statements, the second part of a for
statement can contain at most one condition.
Let us go back to the Yahtzee game. Now that we know more about arrays, we can
declare the dice as an array of integers as shown below.
class Yahtzee {
static int [] dice = new i n t [5];
...
}
Although this declaration is correct, it has one minor drawback. The number 5 is a
magic number . It comes from thin air and it is not clear why we chose 5. A better solution
would be to define the number of dice as a constant. The convention is that constants are
written in all capital letters.
class Yahtzee {
static final int NUMBER OF DICE = 5 ;
static int [] dice = new i n t [ NUMBER OF DICE ] ;
...
}
Remember to use the final keyword to define a variable as a constant. Constants can
be assigned once and cannot be modified after that. Good software practices call for all
 
Search WWH ::




Custom Search