Java Utililty Methods Array Search

List of utility methods to do Array Search

Description

The list of methods to do Array Search are organized into topic(s).

Method

booleanisInArray(String needle, String[] haystack)
check to see if the given value is in the array
return Arrays.asList(haystack).contains(needle);
intmemchr(boolean[] ary, int start, int len, boolean find)
memchr
for (int i = 0; i < len; i++) {
    if (ary[i + start] == find)
        return i + start;
return -1;
intmemchr(char[] haystack, int offset, char needle, int num)
memchr
if (num != 0) {
    int p = 0;
    do {
        if (haystack[offset + p] == needle)
            return 1;
        p++;
    } while (--num != 0);
return 0;
intmemchr(T[] ptr, T value)
memchr
for (int c = 0; c < ptr.length; c++) {
    if (value.equals(ptr[c]))
        return c;
    if (value.hashCode() == ptr[c].hashCode())
        return c;
return -1;
StringreplaceAll(String text, char[] toSearch, char toReplace)
Replaces all occurrences of a search sign with the replacement sign.
if (text != null && toSearch != null) {
    Arrays.sort(toSearch);
    char[] signs = text.toCharArray();
    for (int i = 0; i < signs.length; i++) {
        int index = Arrays.binarySearch(toSearch, signs[i]);
        if (index >= 0) {
            signs[i] = toReplace;
    return new String(signs);
} else {
    return text;
int[]search(double[] input, int[] bounds, double x)
Recursive binary search.
if (bounds[0] == bounds[1]) {
    return new int[] { bounds[0], bounds[1] };
if (bounds[1] - bounds[0] == 1) {
    return new int[] { bounds[0], bounds[1] };
if (input[bounds[0]] == x) {
    return new int[] { bounds[0], bounds[0] };
...
intSearch(int[] in, int val)
Search
return Search(in, val, true);
intsearchFirst(String[] target, String key)
search First
if (isEmpty(target) || key == null) {
    return -1;
for (int i = 0; i < target.length; i++) {
    if (key.equals(target[i])) {
        return i;
return -1;
booleansearchInStringArray(String key, String[] data)
search In String Array
Set<String> values = new HashSet<String>(Arrays.asList(data));
return values.contains(key);