OnJava8-Examples/arrays/AlphabeticSearch.java
2016-01-25 18:05:55 -08:00

29 lines
903 B
Java

// arrays/AlphabeticSearch.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Searching with a Comparator
import java.util.*;
import onjava.*;
import static onjava.ArrayShow.*;
public class AlphabeticSearch {
public static void main(String[] args) {
String[] sa = new Rand.String().array(30);
Arrays.sort(sa, String.CASE_INSENSITIVE_ORDER);
show(sa);
int index = Arrays.binarySearch(sa, sa[10],
String.CASE_INSENSITIVE_ORDER);
System.out.println(
"Index: "+ index + "\n"+ sa[index]);
}
}
/* Output:
[bkIna, cQrGs, cXZJo, dLsmw, eGZMm, EqUCB, gwsqP, hKcxr,
HLGEa, HqXum, HxxHv, JMRoE, JmzMs, Mesbt, MNvqe, nyGcF,
ogoYW, OneOE, OWZnT, RFJQA, rUkZP, sgqia, slJrL, suEcU,
uTpnX, vpfFv, WHkjU, xxEAJ, YNzbr, zDyCy]
Index: 10
HxxHv
*/