IT. Expert System.

Java basics

binarySearch()


he java.util.Arrays.binarySearch(int[] a, int key) method searches the specified array of ints for the specified value using the binary search algorithm.The array must be sorted before making this call.If it is not sorted, the results are undefined. /* IN C#*/ The Array type provides a BinarySearch generic method. This method quickly and accurately pinpoints the location of an element in the array. It can be told how to compare elements. It works correctly only on a presorted array.

Syntax:
public static int binarySearch(int[] a, int key)


Example:

/*java example*/
import java.util.Arrays;

public class ArrayDemo {
 
   public static void main(String[] args) {

    // initializing unsorted int array
    int intArr[] = {30,20,5,12,55};

    // sorting array
    Arrays.sort(intArr);

    // let us print all the elements available in list
    System.out.println("The sorted int array is:");
    for (int number : intArr) {
      System.out.println("Number = " + number);
    }

    // entering the value to be searched
    int searchVal = 12;

    int retVal = Arrays.binarySearch(intArr,searchVal);
	
    System.out.println("The index of element 12 is : " + retVal);
  }
}
/*C# Example*/
using System;
class Program
{
    static void Main()
    {
	//
	// Source array that is ordered ascending.
	//
	string[] array = { "a", "e", "m", "n", "x", "z" };
	//
	// Call versions of the BinarySearch method.
	//
	int index1 = Array.BinarySearch(array, "m");
	int index2 = Array.BinarySearch(array, "x");
	int index3 = Array.BinarySearch(array, "E",
	    StringComparer.OrdinalIgnoreCase);
	//
	// Write results.
	//
	Console.WriteLine(index1);
	Console.WriteLine(index2);
	Console.WriteLine(index3);
    }
}


Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 149 / 158732820. Delta: 0.00370 с