java.util.Comparable and java.util.Comparator
interfaces. You'll also see a useful method of the String
class: split, which splits a string into parts based on a
delimeter. We'll use whitespace as the delimiter, but you can use other
characters (or more generally, regular expressions.)
Code for the recitation is in ComparableStuff.java
accessible ComparableStuff.java
and via snarfing. You'll get a copy of the code in recitation.
The output generated
from executing the main method
is shown below.
original ape ant anteater cat dingo monkey mongoose lion leopard goat giraffe snake yak ant anteater ape cat dingo giraffe goat leopard lion mongoose monkey snake yak reversed follows ape ant anteater cat dingo monkey mongoose lion leopard goat giraffe snake yak yak snake monkey mongoose lion leopard goat giraffe dingo cat ape anteater ant length sorted ant ape cat yak goat lion dingo snake monkey giraffe leopard anteater mongoose show split banana watermelon apple lemon mango orange apple banana lemon mango orange watermelon
toString method in
inner class RevString is changed as follows:
reversed follows 3 8 3 5 6 8 4 7 4 7 5 3 3 5 6 8 4 7 4 7 5 3 3 8
main here:
length sorted ant ape cat yak goat lion dingo snake monkey giraffe leopard anteater mongooseExplain this output: why does yak come before goat, why does anteater come after ape, and where would zebra appear in the output if it was in the array
list?
String
method split does in your own words. Note that
the String literal
"\\s+" is a regular-expression meaning 'any whitespace'.
Comparator class named
AlphaLength so that
if used in place of LengthComparator would have the
following characteristics:
Comparator and the array list.
ant ape anteater cat dingo goat giraffe lion leopard monkey mongoose snake yak