// : c11:AlphabeticSearch.java
//Searching with a Comparator.
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
//www.BruceEckel.com. See copyright notice in CopyRight.txt.
import java.util.Arrays;
import java.util.Comparator;
public class AlphabeticSearch {
public static void main(String[] args) {
String[] sa = new String[] { "a", "c", "d" };
AlphabeticComparator comp = new AlphabeticComparator();
Arrays.sort(sa, comp);
int index = Arrays.binarySearch(sa, sa[10], comp);
System.out.println("Index = " + index);
}
} ///:~
class AlphabeticComparator implements Comparator {
public int compare(Object o1, Object o2) {
String s1 = (String) o1;
String s2 = (String) o2;
return s1.toLowerCase().compareTo(s2.toLowerCase());
}
} ///:~
|