java4all@1986 java. Powered by Blogger.

Search with a Comparator

>> Tuesday, May 24, 2011

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 = (Stringo1;
    String s2 = (Stringo2;
    return s1.toLowerCase().compareTo(s2.toLowerCase());
  }
///:~

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP