Ordered Array of Strings

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

Yes.

Ordered Array of Strings

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 13:

Does String implement Comparable?