How to Perform Binary Search on Double Array in Java


In this example we will show how to perform binary search for an element of double array with Arrays class in Java.

Source Code

package com.beginner.examples;

import java.util.Arrays;

public class BinarySearchDoubleArray {

	public static void main(String[] args) {
		
		//create double array.
		double doubleArray[] = {5.21,3.14,9.96,5.32};
		
		/*
		 * Please note that the double array MUST BE SORTED before it can be searched
		 * using binarySearch method.
		 */
		Arrays.sort(doubleArray);
		int result = Arrays.binarySearch(doubleArray,9.96);
		System.out.println("Result of binary search of 9.96 is : " + result);

	}

}

Output:

Result of binary search of 9.96 is : 3

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments