Performing Binary Search on Java char Array - Java Language Basics

Java examples for Language Basics:char

Description

Performing Binary Search on Java char Array

Demo Code


import java.util.Arrays;
 
public class Main {
 
  public static void main(String[] args) {
    char charArray[] = {'a','b','d','e'};
    Arrays.sort(charArray);// w w w.  j  a va 2 s  . co  m
    char searchValue = 'b';

    int intResult = Arrays.binarySearch(charArray,searchValue);
    System.out.println("Result of binary search of 'b' is : " + intResult);
   
    searchValue = 'c';
    intResult = Arrays.binarySearch(charArray,searchValue);
    System.out.println("Result of binary search of 'c' is : " + intResult);
 
  }
}

Result


Related Tutorials