"Hell" < "Hello" < "Hill"
Comparable
interface
public interface Comparable { int compareTo(Object otherObject) }
a.compareTo(b)
returns a negative int
if a < b
, a positive int
if a > b
, and zero if they are the same/** A country with a name and area. */ public class Country implements Comparable { /** Constructs a country. @param aName the name of the country @param anArea the area of the country */ public Country(String aName, double anArea) { name = aName; area = anArea; } /** Gets the name of the country. @return the name */ public String getName() { return name; } /** Gets the area of the country. @return the area */ public double getArea() { return area; } /** Compares two countries by area. @param other the other country @return a negative number if this country has a smaller area than otherCountry, 0 if the areas are the same, a positive number otherwise */ public int compareTo(Object otherObject) { Country other = (Country) otherObject; if (area < other.area) return -1; if (area > other.area) return 1; return 0; } private String name; private double area; }
compareTo
public int compareTo(Object otherObject) { Country other = (Country) otherObject; if (area < other.area) return -1; if (area > other.area) return 1; return 0; }
public class Country implements Comparable<Country> { ... public int compareTo(Country other) { if (area < other.area) return -1; if (area > other.area) return 1; return 0; } }
public class Util { public static Comparable smallest(Comparable[] values) { Comparable smallestSoFar = values[0]; for (int i = 1; i < values.length; i++) if (values[i].compareTo(smallestSoFar) < 0) smallestSoFar = values[i]; return smallestSoFar; } } public class SmallestDemo { public static void main(String[] args) { String[] names = { "Fred", "Wilma", "Barney" }; Comparable first = Util.smallest(names); System.out.println(first); } }
What will this program print?
public class SmallestDemo { public static void main(String[] args) { String[] names = { "Fred", "Wilma", "Barney" }; String first = Util.smallest(names); System.out.println(first); } }
What is the advantage of using the Comparable
interface in the Util.smallest
method?
Util.smallest(String[] values)
Util.smallest(Measurable[] values)
Util.smallest(Object[] values)
Comparable
interface, e.g. Country
objectsCountry
implements Comparable
Country
to Util.smallest
or Arrays.sort
Comparable
whenever the sort order changespublic interface Comparator { int compare(Object object1, Object object2); }
import java.util.*; public class CountryComparatorByName implements Comparator { public int compare(Object object1, Object object2) { Country country1 = (Country) object1; Country country2 = (Country) object2; return country1.getName().compareTo(country2.getName()); } }
Which of the following sorts an array of countries by name?
Arrays.countryComparatorByName(countries);
Arrays.sort(
countries, CountryComparatorByName);
Arrays.sort(countries, new CountryComparatorByName());
Arrays.sort(new CountryComparatorByName(countries));
public class Util { public static Object smallest(Object[] values, Comparator comp) { Object smallestSoFar = values[0]; for (int i = 1; i < values.length; i++) if (comp.compare(values[i], smallestSoFar) < 0) smallestSoFar = values[i]; return smallestSoFar; } }
class Outer { class Inner { ... } void someMethod() { Inner fred = new Inner(); ... } }
class Outer { void someMethod() { class Inner { ... } Inner fred = new Inner(); ... } }
Arrays.sort
Comparator
insteadComparator
public static void main(String[] args) { String[] names = { "Fred", "Wilma", "Barney" }; class LengthComparator implements Comparator { public int compare(Object o1, Object o2) { return ((String) o1).length() - ((String) o2).length(); } } Arrays.sort(names, new LengthComparator()); }
Arrays.sort(names, new LengthComparator());
Comparator fred = new LengthComparator(); Arrays.sort(names, fred);
Arrays.sort(names, new Comparator() { public int compare(Object o1, Object o2) { return ((String) o1).length() - ((String) o2).length(); } });
Let's make Item
an inner class of ArrayListAddressBook
(from the lab):
public class ArrayListAddressBook { private class Item { String name; String key; String value; } ... }
Which of the following is true?
ArrayListAddressBook
class can construct and access Item
objectsArrayListAddressBook
class refers to the inner class as ArrayListAddressBook.Item
Item
class is poorly designed because the instance variables of Item
should have been declared as private
Item
class should have been added to the AddressBook
interface instead of the ArrayListAddressBook
classAddressBook
public class MockAddressBook implements AddressBook { public void load(String sourceName) {} public void save() {} public void get(String name, String key) { return "Fred"; } public void put(String name, String key, String value) {} public void remove(String name, String key) {} }
AddressBook book = new MockAddressBook()
to
AddressBook book = new ArrayListAddressBook()