Java Reference
In-Depth Information
indexOfNextSmallest =
indexOfSmallest(index, a);
interchange(index,indexOfNextSmallest, a);
//a.get(0), a.get(1),...,a.get(index)
//are sorted. The rest of the
//elements are in the remaining positions.
}
}
/**
Precondition: i and j are legal indices for the ArrayList a.
Postcondition: The values of a.get(i) and
a.get(j) have been interchanged.
*/
private static void interchange(
int i, int j, ArrayList<String> a)
{
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 , dou-
ble , or char . You can, however, have an ArrayList with base type Integer that
can be used to store integers.
Search WWH ::




Custom Search