Java Reference
In-Depth Information
int
largest = Integer.MIN_VALUE;
for
(
int
element : arg)
if
(element > largest)
largest = element;
return
largest;
}
17. The last line would change to the following:
I ate cod, broccoli, , peanuts, and apples.
18.
public void
removeAll( )
{
numberUsed = 0;
}
19.
public void
increaseCapacity(
int
newCapacity)
{
if
(newCapacity > numberUsed)
{
maxNumberElements = newCapacity;
double
[] temp =
new double
[newCapacity];
for
(
int
i = 0; i < a.length; i++)
temp[i] = a[i];
a = temp;
}
//else do nothing.
}
20. All you need to do to make your code work for sorting into decreasing order is to
replace the with in the following line of the definition of
indexOfSmallest
:
if
(a[index] < min)
However, to make your code easy to read, you should also rename the method
indexOfSmallest
to
indexOfLargest
, rename the variable
min
to
max
, and
rename the variable
indexOfMin
to
indexOfMax
. You should rewrite some of the
comments to reflect these changes as well.
21. If an array has a value that occurs more than once and you sort the array using the
method
SelectionSort.sort
, then there will be as many copies of the repeated
value after the array is sorted as there originally were in the array.
22. We give two slightly different versions. Both versions are on the accompanying
website.
extra code
on website
import
java.util.Scanner;
public class
ForEachEnumDemo
{
enum
WorkDay {MONDAY, TUESDAY, WEDNESDAY, THURSDAY,FRIDAY};
public static void
main(String[] args)
{
WorkDay[] day = WorkDay.values( );