Java Reference
In-Depth Information
Because next is 1 , this line of code becomes
count[1]++;
So the counter at index [1] is incremented:
[0]
[1]
[2]
[3]
[4]
count
0
1
0
0
0
Then a 4 is read from the input file, which means count[4] is incremented:
[0]
[1]
[2]
[3]
[4]
count
0
1
0
0
1
Next, another 1 is read from the input file, which increments count[1] :
[0]
[1]
[2]
[3]
[4]
count
0
2
0
0
1
Then a 0 is read from the input file, which increments count[0] :
[0]
[1]
[2]
[3]
[4]
count
1
2
0
0
1
Notice that in just this short set of data you've jumped from index 1 to index 4,
then back down to index 1, then to index 0. The program continues executing in this
manner, jumping from counter to counter as it reads values from the file. This ability
to jump around in the data structure is what's meant by random access.
After processing all of the data, the array ends up looking like this:
[0]
[1]
[2]
[3]
[4]
count
5
9
6
9
11
After this loop finishes executing, you can report the total for each score by using
the standard traversing loop with a println :
for (int i = 0; i < count.length; i++) {
System.out.println(i + "\t" + count[i]);
}
 
Search WWH ::




Custom Search