Java Reference
In-Depth Information
{
String temp;
temp = a.get(i);
a.set(i, a.get(j));
a.set(j, temp);
}
/**
Returns the index of the lexicographically first value among
a.get(startIndex), a.get(startIndex+1),...,a.get(a.size()
—
1)
*/
private static int
indexOfSmallest(
int
startIndex, ArrayList<String> a)
{
String min = a.get(startIndex);
int
indexOfMin = startIndex;
int
index;
for
(index = startIndex + 1; index < a.size(); index++)
if
((a.get(index)).compareTo(min) < 0)
{
min = a.get(index);
indexOfMin = index;
}
return
indexOfMin;
}
}
10. No, the base type of an
ArrayList
cannot be a primitive type, such as
int
,
double
, or
char
. You can, however, have an
ArrayList
with base type
Integer
that can be used to store integers.
11. Notice that the following, while correct, is not as easy to understand as the
for-each loop.
for
(
int
i; i < a.size(); i++)
System.out.println(a.get(i) + " differs from average by "
+ (a.get(i) average));
12.
public static
<T>
int
getMidindex(T[] a)
{
return
a.length/2;
}
13. This is a trick question. As we explained in the text, you cannot have an array of
type
UnorderedPair<String>[]
or of type
Pair<String>[]
.