Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use binary search method in java
3332 Views
BinarySearchExample
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class BinarySearchExample {
    public static void main(String args[]){
        List<String> dataList = new ArrayList<String>();
            dataList.add("Ball");
            dataList.add("Apple");
            dataList.add("Dog");
            dataList.add("Cat");
         
        //Before doing binary search list should be sorted..
        Collections.sort(dataList);
         
        //Data list Object sorted..
        System.out.println(dataList);
         
        //Binarysearch method to search a string from list of strings
        System.out.println(Collections.binarySearch(dataList, "Ball"));
        System.out.println(Collections.binarySearch(dataList, "Eagle"));
        System.out.println(Collections.binarySearch(dataList, "Apple"));
    }
}
Output 
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details