Java Reference
In-Depth Information
a parameter corresponding to the array
candidatesName
. Essentially,
this
method is:
public static void
getCandidatesName(Scanner inp,
String[] cNames)
{
int
i;
for
(i = 0; i < cNames.length; i++)
cNames[i] = inp.next();
}
After a call to this method, the arrays to hold the data are as shown in Figure 14-17.
candidatesName
votesByRegion
totalVotes
[0]
0
0
0
0
0
0
[1]
0
0
0
0
0
0
[2]
0
0
0
0
0
0
[3]
0
0
0
0
0
0
[0]
0
0
0
0
0
0
[0]
Mia
Mickey
Donald
Peter
Danny
Ashley
[0]
[1]
[1]
[1]
[2]
[2]
[2]
[3]
[3]
[3]
[4]
[4]
[4]
[5]
[5]
[5]
FIGURE 14-17
Arrays
candidatesName
,
votesByRegion
, and
totalVotes
after reading
candidates' names
This method uses a selection sort algorithm to sort the array
candidatesName
. This
method has only one parameter: the parameter corresponding to the array
candidatesName
. Essentially, this method is:
Method
sortCandidates
Name
public static void
sortCandidatesName(String[] cNames)
{
int
i, j;
int
min;
String temp;
//selection sort
for
(i = 0; i < cNames.length - 1; i++)
{
min = i;
for
(j = i + 1; j < cNames.length; j++)
if
(cNames[j].compareTo(cNames[min]) <
0)
min = j;
Search WWH ::
Custom Search