Java search byte in byte array

Java.util.Arrays.binarySearch() Method

The java.util.Arrays.binarySearch(byte[] a, int fromIndex, int toIndex, byte key) method searches a range of the specified array of bytes for the specified value using the binary search algorithm. The range must be sorted before making this call.If it is not sorted, the results are undefined.

Declaration

Following is the declaration for java.util.Arrays.binarySearch() method

public static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)

Parameters

  • a − This is the array to be searched.
  • fromIndex − This is the index of the first element (inclusive) to be searched.
  • toIndex − This is the index of the last element (exclusive) to be searched.
  • key − This is the value to be searched for.

Return Value

This method returns index of the search key, if it is contained in the array, else it returns (-(insertion point) — 1). The insertion point is the point at which the key would be inserted into the array; the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key.

Exception

  • IllegalArgumentException − if fromIndex > toIndex
  • ArrayIndexOutOfBoundsException − if fromIndex < 0 or toIndex >a.length

Example

The following example shows the usage of java.util.Arrays.binarySearch() method.

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing unsorted byte array byte byteArr[] = ; // sorting array Arrays.sort(byteArr); // let us print all the elements available in list System.out.println("The sorted byte array is:"); for (byte number : byteArr) < System.out.println("Number = " + number); >// entering the value to be searched byte searchVal = 35; // entering the range of index int retVal = Arrays.binarySearch(byteArr,2,5,searchVal); System.out.println("The index of element 35 is : " + retVal); > >

Let us compile and run the above program, this will produce the following result −

The sorted byte array is: Number = 10 Number = 15 Number = 20 Number = 22 Number = 35 The index of element 35 is : 4

Источник

Java Arrays — binarySearch() Method

The Java Arrays binarySearch(byte[] a, byte key) method searches the specified array of bytes 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.

Читайте также:  Print file content in python

Declaration

Following is the declaration for java.util.Arrays.binarySearch(byte[] a, byte key) method

public static int binarySearch(byte[] a, byte key)

Parameters

  • a − This is the array to be searched.
  • key − This is the value to be searched for.

Return Value

This method returns index of the search key, if it is contained in the array, else it returns (-(insertion point) — 1). The insertion point is the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key.

Exception

Java Arrays binarySearch(byte[] a, int fromIndex, int toIndex, byte key) Method

Description

The Java Arrays binarySearch(byte[] a, int fromIndex, int toIndex, byte key) method searches a range of the specified array of bytes for the specified value using the binary search algorithm. The range must be sorted before making this call.If it is not sorted, the results are undefined.

Declaration

Following is the declaration for java.util.Arrays.binarySearch(byte[] a, int fromIndex, int toIndex, byte key) method

public static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)

Parameters

  • a − This is the array to be searched.
  • fromIndex − This is the index of the first element (inclusive) to be searched.
  • toIndex − This is the index of the last element (exclusive) to be searched.
  • key − This is the value to be searched for.

Return Value

This method returns index of the search key, if it is contained in the array, else it returns (-(insertion point) — 1). The insertion point is the point at which the key would be inserted into the array; the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key.

Exception

  • IllegalArgumentException − if fromIndex > toIndex
  • ArrayIndexOutOfBoundsException − if fromIndex < 0 or toIndex >a.length

Example 1

The following example shows the usage of Java Arrays binarySearch(byte[], key) method. First, we’ve created an array of bytes, sorted and printed them. Then binary search is performed on a value and result is printed.

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing unsorted byte array byte byteArr[] = ; // sorting array Arrays.sort(byteArr); // let us print all the elements available in list System.out.println("The sorted byte array is:"); for (byte number : byteArr) < System.out.println("Number = " + number); >// entering the value to be searched byte searchVal = 35; int retVal = Arrays.binarySearch(byteArr,searchVal); System.out.println("The index of element 35 is : " + retVal); > >

Output

Let us compile and run the above program, this will produce the following result −

The sorted byte array is: Number = 10 Number = 15 Number = 20 Number = 22 Number = 35 The index of element 35 is : 4

Example 2

The following example shows the usage of Java Arrays binarySearch(byte[], fromIndex, toIndex, key) method. First, we’ve created an array of bytes, sorted and printed them. Then binary search is performed on a value on sub array and result is printed.

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing unsorted byte array byte byteArr[] = ; // sorting array Arrays.sort(byteArr); // let us print all the elements available in list System.out.println("The sorted byte array is:"); for (byte number : byteArr) < System.out.println("Number = " + number); >// entering the value to be searched byte searchVal = 35; // entering the range of index int retVal = Arrays.binarySearch(byteArr,2,5,searchVal); System.out.println("The index of element 35 is : " + retVal); > >

Output

Let us compile and run the above program, this will produce the following result −

The sorted byte array is: Number = 10 Number = 15 Number = 20 Number = 22 Number = 35 The index of element 35 is : 4

Example 3

The following example shows the usage of Java Arrays binarySearch(byte[], key) method. First, we’ve created an array of bytes, sorted and printed them. Then binary search is performed on a value which is not present in the array and result is printed .

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing unsorted byte array byte byteArr[] = ; // sorting array Arrays.sort(byteArr); // let us print all the elements available in list System.out.println("The sorted byte array is:"); for (byte number : byteArr) < System.out.println("Number = " + number); >// entering the value to be searched byte searchVal = 38; int retVal = Arrays.binarySearch(byteArr,searchVal); System.out.println("The index of element 38 is : " + retVal); > >

Output

Let us compile and run the above program, this will produce the following result −

The sorted byte array is: Number = 10 Number = 15 Number = 20 Number = 22 Number = 35 The index of element 38 is : -6

Источник

Читайте также:  Relativelayout android in java

Find a key by means of a binary search in byte array. — Java java.lang

Find a key by means of a binary search in byte array.

Demo Code

/*/*from w ww . jav a 2 s. co m*/ * DomUI Java User Interface - shared code * Copyright (c) 2010 by Frits Jalvingh, Itris B.V. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * See the "sponsors" file for a list of supporters. * * The latest version of DomUI and related code, support and documentation * can be found at http://www.domui.org/ * The contact for the project is Frits Jalvingh . */ import java.sql.*; import java.util.*; public class Main< public static void main(String[] argv) throws Exception< List v = java.util.Arrays.asList("asdf","java2s.com"); int start = 2; byte[] key = new byte[]; System.out.println(findKey(v,start,key)); > /** * Find a key by means of a binary search. * post: index if found, -1 if not * * @param v vector containing the element to search for * @param start location where to start comparing the bytes * @param key The key to look for. */ public static int findKey(Listbyte[]> v, int start, byte[] key) < byte[] ba; int mid, rv; int low, high; //-- Init, low = 0; high = v.size(); //-- Loop till found. for (;;) < if (low >= high) return -1; //-- Get middle element, mid = (low + high) / 2; ba = v.get(mid); // Get middle element, rv = ByteArrayUtil.compare(ba, start, key.length, key); // Compare, if (rv == 0) return mid; if (rv < 0) < high = mid; >else < low = mid + 1; >> > /** * Compares a byte array part with an array. */ public static int compare(byte[] a, int from, int to, byte[] b) < // if(b.length != to-from) throw new IllegalArgumentException("Bad size array passed to compare."); int sx = 0; int rv = 0; for (int i = from; i < to; i++, sx++) < if (sx < b.length) rv = (a[i] & 0xff) - (b[sx] & 0xff); else rv = a[i]; if (rv != 0) < return rv > 0 ? -1 : 1; > > return 0; > static public int compare(byte[] a, int astart, byte[] b, int bstart, int len) < while (len-- > 0) < int rv = a[astart++] - b[bstart++]; if (rv < 0) return -1; else if (rv > 0) return 1; > return 0; > >

Источник

Оцените статью