adsense


Showing posts with label JAVA EXAMPLES VECTOR BINARY. Show all posts
Showing posts with label JAVA EXAMPLES VECTOR BINARY. Show all posts

Friday, 30 October 2015

JAVA EXAMPLES VECTOR BINARY PAGE 151

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES VECTOR BINARY






Java Examples - Vector Binary



Problem Description:
How to execute binary search on a vector ?
Solution:
Following example how to execute binary search on a vector with the help of v.add() method of Vector class and sort.Collection() method of Collection class.
import java.util.Collections;
import java.util.Vector;

public class Main {
   public static void main(String[] args) {
      Vector v = new Vector();
      v.add("X");
      v.add("M");
      v.add("D");
      v.add("A");
      v.add("O");
      Collections.sort(v);
      System.out.println(v);
      int index = Collections.binarySearch(v, "D");
      System.out.println("Element found at : " + index);
   }
}
Result:
The above code sample will produce the following result.
[A, D, M, O, X]
Element found at : 1