Activity 9.9.1.
Run the code below a couple times to populate the array with random numbers and see the result of the
Arrays.sort()
method.int
s, you can’t use it to sort double
s.Integer
and Double
, together with the java.lang.Comparable
interface, which is specially designed for this purpose.Comparable
Interface
java.lang.Comparable
interface consists of the compareTo() method:public abstract interface Comparable {
public int compareTo (Object o); // Abstract method}
compareTo()
, a class can impose an order on its objects. The Comparable
interface is implemented by all of Java’s wrapper classes — that is, by Integer
, Double
, Float
, Long
, and so on (Fig. 9.24).Integer
is both an Object and a Comparable
. One implication of this is that an Integer
can be used in any method that takes either an Object
parameter or a Comparable
parameter.compareTo()
method takes an Object
parameter and returns an int
. It is meant to be invoked as o1.compareTo(o2)
, where o1 and o2 are objects of the same type. Classes that implement compareTo()
must abide by the following rules for its return value:if (o1 < o2) then o1.compareTo(o2) < 0
if (o1.equals(o2)) then o1.compareTo(o2) == 0
if (o1 > o2) then o1.compareTo(o2) > 0
o1 < o2
, then o1.compareTo(o2)
will return a negative integer. If o1 > o2
, then o1.compareTo(o2)
will return a positive integer. And if o1
and o2
are equal, then o1.compareTo(o2)
will return 0.Comparable
, we can use the compareTo() method to help sort its elements. For example, the following revised version of insertionSort()
method can be used to sort any array of Comparable
objects—that is, any array of objects whose class implements Comparable
for any type T
. public <T extends Comparable<T>> void sort(T[] arr) {
T temp; // Temporary variable for insertion
for (int k = 1; k < arr.length; k++) {
temp = arr[k]; // Remove it from array
int i;
for (i = k-1; i >= 0 && arr[i].compareTo(temp) > 0; i--)
arr[i+1] = arr[i]; // Move it right by one
arr[i+1] = temp; // Insert the element
}
} // sort()
T
. Thus, we can pass it an array of Integer
or Float
, and so on. Then, to compare elements of a Comparable
array, we use the compareTo()
method:for (i = k-1; i >= 0 && arr[i].compareTo(temp) > 0; i--)
int
s, as in the original insertion sort. Indeed, it doesn’t mention the specific type—Integer
, Float
, or whatever — of the objects that it is sorting. It refers only to Comparable
s. Therefore, we can use this method to sort any type of object, as long as the object’s class implements the Comparable
interface. Thus, by using Comparable
, we have a more general insertionSort()
method, one that can sort any one-dimensional array of Comparable
s.TestSort
class (Figure 9.9.2 and Listing 9.9.3) provides an example of how to use the polymorphic sort()
method.sort()
method that we just described; a polymorphic print()
method, which can be used to print the values of any array of Comparable
; and a main()
method. The main()
method creates arrays of Integer
and Float
and then uses the polymorphic sort()
method to sort them. Note how the print()
method uses the polymorphic toString()
method to print the elements of a Comparable
array.Integer
and Float
classes use class inheritance to inherit features from the Object
class, and they use interface implementation to inherit the compareTo()
method from the Comparable
class. By implementing versions of the toString()
and compareTo()
methods that are appropriate for these wrapper classes, Java makes it easier to use Integer
and Float
objects in a variety of contexts.sort()
method that can sort an array containing any kind of object as long as the object implements the Comparable
interface.java.util.Arrays.sort()
Method
java.util.Arrays
class contains a polymorphic sort method that is very simple to use. For example, here’s how we would use it to sort the two arrays declared in the TestSort
program:java.util.Arrays.sort(iArr);
// or if you: import java.util.*;
Arrays.sort(fArr);
Arrays.sort()
method.compareTo()
method to the LetterFreq
class so that it implements the Comparable
interface. The method should define one object to be less than another object if its freq
instance variable is less.sort()
method that can be added to the definition of the AnalyzeFreq
class. Make it so the array in the class can be sorted into ascending order using the ordering of LetterFreq
defined in the previous exercise. Use the java.util.Arrays.sort()
method.main()
of the AnalyzeFreq
class to make use of the sort()
method of the previous exercise.