Java Reference
In-Depth Information
6. The index for add (that is, index2 ) is allowed to be one larger than the index for
set (that is, index1 ). The index for set must be strictly less than the size of the
ArrayList . The index for add can also be equal to the size of the ArrayList .
7. Yes. The ArrayList can contain more than 20 elements. The number 20 used as
an argument to the constructor merely gives the initial memory allocation for the
ArrayList . More memory is automatically allocated when it is needed.
8 . for (Double element : numberList)
System.out.println(element);
9 . import java.util.ArrayList;
/**
Class for sorting an ArrayList of Strings lexicographically
(approximately alphabetically).
*/
public class StringSelectionSort
{
/**
Sorts the ArrayList a so that a.get(0), a.get(1),...,
a.get(a.size() 1) are in lexicographic order.
*/
public static void sort(ArrayList<String> a)
{
int index, indexOfNextSmallest;
for (index = 0; index < a.size() 1; index++)
{ //Place the correct value in position index:
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)
Search WWH ::




Custom Search