Close

Java Collections - Arrays.binarySearch() Examples

Java Collections Java Java API 


Class:

java.util.Arrays

java.lang.Objectjava.lang.Objectjava.util.Arraysjava.util.ArraysLogicBig

Methods:

These methods searches the specified array for the specified value using the binary search algorithm.

public static int binarySearch(long[] a,
                               long key)
public static int binarySearch(long[] a,
                               int fromIndex,
                               int toIndex,
                               long key)
public static int binarySearch(int[] a,
                               int key)
public static int binarySearch(int[] a,
                               int fromIndex,
                               int toIndex,
                               int key)
public static int binarySearch(short[] a,
                               short key)
public static int binarySearch(short[] a,
                               int fromIndex,
                               int toIndex,
                               short key)
public static int binarySearch(char[] a,
                               char key)
public static int binarySearch(char[] a,
                               int fromIndex,
                               int toIndex,
                               char key)
public static int binarySearch(byte[] a,
                               byte key)
public static int binarySearch(byte[] a,
                               int fromIndex,
                               int toIndex,
                               byte key)
public static int binarySearch(double[] a,
                               double key)
public static int binarySearch(double[] a,
                               int fromIndex,
                               int toIndex,
                               double key)
public static int binarySearch(float[] a,
                               float key)
public static int binarySearch(float[] a,
                               int fromIndex,
                               int toIndex,
                               float key)
public static int binarySearch(Object[] a,
                               Object key)
public static int binarySearch(Object[] a,
                               int fromIndex,
                               int toIndex,
                               Object key)
public static <T> int binarySearch(T[] a,
                                   T key,
                                   Comparator<? super T> c)
public static <T> int binarySearch(T[] a,
                                   int fromIndex,
                                   int toIndex,
                                   T key,
                                   Comparator<? super T> c)

Examples


package com.logicbig.example.arrays;

import java.util.Arrays;

public class BinarySearchExample {

public static void main(String... args) {
int[] arr = {1, 2, 3, 4, 6, 7};
int i = Arrays.binarySearch(arr, 6);
System.out.println(i);
}
}

Output

4




package com.logicbig.example.arrays;

import java.util.Arrays;

public class BinarySearchExample2 {


public static void main(String... args) {
int[] arr = {1, 2, 3, 4, 6, 7};
int i = Arrays.binarySearch(arr, 2, 5, 6);
System.out.println(i);
}
}

Output

4




package com.logicbig.example.arrays;

import java.util.Arrays;

public class BinarySearchExample4 {


public static void main(String... args) {
char[] arr = {'a', 'b', 'c', 'd', 'e', 'f'};
int i = Arrays.binarySearch(arr, 'd');
System.out.println(i);
}
}

Output

3




package com.logicbig.example.arrays;

import java.util.Arrays;

public class BinarySearchExample5 {


public static void main(String... args) {
String[] arr = {"one", "two", "four", "three"};
//arr must be in sorted order for binarySearch to work
Arrays.sort(arr);
int i = Arrays.binarySearch(arr, "three");
System.out.println(i);
}
}

Output

2

package com.logicbig.example.arrays;

import java.util.Arrays;
import java.util.Comparator;

public class BinarySearchExample6 {


public static void main(String... args) {
String[] arr = {"1", "2", "3", "4"};

Arrays.sort(arr, Comparator.reverseOrder());

int i = Arrays.binarySearch(arr, "4", Comparator.reverseOrder());
System.out.println(i);

i = Arrays.binarySearch(arr, "1", Comparator.reverseOrder());
System.out.println(i);
}
}

Output

0
3




package com.logicbig.example.arrays;

import java.util.Arrays;

public class BinarySearchExample7 {


public static void main(String... args) {
MyClass[] arr = {MyClass.of("3"), MyClass.of("2"),
MyClass.of("1"), MyClass.of("4")};

Arrays.sort(arr);

int i = Arrays.binarySearch(arr, MyClass.of("4"));
System.out.println(i);

i = Arrays.binarySearch(arr, MyClass.of("2"));
System.out.println(i);
}

private static class MyClass implements Comparable<MyClass> {
private String myStr;

static MyClass of(String str) {
MyClass myClass = new MyClass();
myClass.myStr = str;
return myClass;
}

public String getMyStr() {
return myStr;
}

@Override
public int compareTo(MyClass o) {
return myStr.compareTo(o.myStr);
}
}
}

Output

3
1




See Also