go to previous page   go to home page   go to next page

Answer:

Yes.


Ordered Array of Strings

ant, bat, cat, dog, elf, fox

Here is a picture of an ordered array. Each cell of the array holds a reference to a String. The order of the Strings is determined by the values returned by the compareTo() method for Strings.

For any two strings X and Y, if X.compareTo(Y) is negative, then X is left of Y in the ordered array.

For an array of object references, the sort() method automatically uses the compareTo() method of the class of objects being sorted. The class must implement the Comparable interface (and so must have a compareTo() method).

static void sort( Object[] a )   

// Sorts the array a into ascending order,
// as determined by the values compareTo() returns.
// Each cell of the array must refer to an object
// (must not be null).

If not all cells of the array are filled, use

static void sort( Object[] a, 
                  int fromIndex, int toIndex )   

// Sorts the array a into ascending order,
// as determined by the values compareTo() returns.
//
// Only the cells between the two indexes are sorted.
// Each of those cells  must refer to an object.

QUESTION 15:

Does String implement Comparable?


go to previous page   go to home page   go to next page