Multi-Dimensional Arrays

We can expand the whole idea of arrays to multiple dimensions, beyond one or two dimensions. Read these sections to see how this is done.

The java.util.Arrays.sort() Method

While sorting algorithms provide a good way to introduce the concepts of array processing, real-world programmers never write their own sort algorithms. Instead they use library methods, which have been written and optimized by programming experts. Moreover, library sort routines use sort algorithms that are much more efficient than the ones we’ve discussed.

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

Annotation 2020-03-26 214613

That’s all there is to it! Obviously, learning how to use Java’s class and method library, saves real-word programmers lots of effort.


SELF-STUDY EXERCISES 

EXERCISE 9.20 Add a definition of a 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. 

EXERCISE 9.21 Add a definition of a 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. 

EXERCISE 9.22 Rewrite the main() of the AnalyzeFreq class to make use of the sort() method of the previous exercise.