Java Array Search arraySearch(byte[] array, byte[] search)

Here you can find the source of arraySearch(byte[] array, byte[] search)

Description

array Search

License

Open Source License

Declaration

public static int arraySearch(byte[] array, byte[] search) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public static int arraySearch(byte[] array, byte[] search) {
        if (search.length == 0) {
            return -1;
        }//from www .j a v  a 2  s . co  m

        int found = 0;
        loop: while (true) {
            found = binarySearch(array, search[0], found + 1);
            if (found < 0 || found > array.length) {
                return -1;
            }

            for (int i = 1; i < search.length; i++) {
                try {
                    if (array[found + i] != search[i]) {
                        continue loop;
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                    return -1;
                }
            }
            break;
        }

        return found;
    }

    public static int binarySearch(byte[] a, byte key, int startPos) {
        if (startPos > a.length || startPos < 0) {
            return -1;
        }

        for (int i = startPos; i < a.length; i++) {
            if (a[i] == key) {
                return i;
            }
        }
        return -1;
    }
}

Related

  1. arraySearch(int[] arr, int i)
  2. byteArraySearch(byte[] data, byte[] searchData)
  3. contains(byte[] array, byte toSearch)
  4. getFoundIdx(char to_find, char[] to_search, boolean do_orderAsc)