Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. // Java program to demonstrate working of Collections.
  2. // binarySearch()
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6.  
  7. public class GFG
  8. {
  9. public static void main(String[] args)
  10. {
  11. List<Integer> al = new ArrayList<Integer>();
  12. al.add(1);
  13. al.add(2);
  14. al.add(3);
  15. al.add(10);
  16. al.add(20);
  17.  
  18. // 10 is present at index 3.
  19. int key = 10;
  20. int res = Collections.binarySearch(al, key);
  21. if (res >= 0)
  22. System.out.println(key + " found at index = "
  23. + res);
  24. else
  25. System.out.println(key + " Not found");
  26.  
  27. key = 15;
  28. res = Collections.binarySearch(al, key);
  29. if (res >= 0)
  30. System.out.println(key + " found at index = "
  31. + res);
  32. else
  33. System.out.println(key + " Not found");
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement