2015-09-07 11:44:36 -06:00
|
|
|
// arrays/AlphabeticSearch.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
// Searching with a Comparator.
|
|
|
|
import java.util.*;
|
2015-11-11 20:20:04 -08:00
|
|
|
import onjava.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
public class AlphabeticSearch {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
String[] sa = Generated.array(new String[30],
|
2015-11-03 12:00:44 -08:00
|
|
|
new RandomSupplier.String(5));
|
2015-06-15 17:47:35 -07:00
|
|
|
Arrays.sort(sa, String.CASE_INSENSITIVE_ORDER);
|
|
|
|
System.out.println(Arrays.toString(sa));
|
|
|
|
int index = Arrays.binarySearch(sa, sa[10],
|
|
|
|
String.CASE_INSENSITIVE_ORDER);
|
|
|
|
System.out.println("Index: "+ index + "\n"+ sa[index]);
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
[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
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|