Java Reference
In-Depth Information
To process this entry, we access row number 2 of the array votesByRegion . Because
Donald received 35 votes from Region 2, we access row number 2 and column
number 1 , that is, votesByRegion[2][1] , and update this entry by adding 35 to its
previous value. The following statement accomplishes this:
votesByRegion[2][1] = votesByRegion[2][1] + 35;
After processing this entry, the three arrays are as shown in Figure 14-21.
Donald
region = 2
candidatesName
votesByRegion
totalVotes
[0]
0
10
76
0
80
100
[1]
0
0
48
45
0
0
[2]
50
56
0
0
78
0
[3]
0
0
0
0
0
20
[0]
0
0
0
0
0
0
[0]
Ashley
Danny
Donald
Mia
Mickey
Peter
[0]
[1]
[1]
[1]
[2]
[2]
[2]
[3]
[3]
[3]
[4]
[4]
[4]
[5]
[5]
[5]
FIGURE 14-21 Arrays candidatesName , votesByRegion ,and totalVotes after
processing the entry Donald235
We now describe the method binSearch and the method processVotes to process
the voting data.
This method implements the binary search algorithm on the array candidatesName .
It is similar to the method binarySearch . Its definition is:
Method
binSearch
public static int binSearch(String[] cNames, String name)
{
int first, last;
int mid = 0;
boolean found;
first = 0;
last = cNames.length - 1;
found = false ;
1
4
while (first <= last && !found)
{
mid = (first + last) / 2;
Search WWH ::




Custom Search