Java Array Search isIn(char to_find, char[] to_search)

Here you can find the source of isIn(char to_find, char[] to_search)

Description

Is the char in the char-array?.

License

LGPL

Return

( getFoundIdx (to_find, to_search) > -1)

Declaration

public static final boolean isIn(char to_find, char[] to_search) 

Method Source Code


//package com.java2s;
/*license*\//from w w w  .  j  a  v a 2 s  .  c om
   XBN-Java: Copyright (C) 2014, Jeff Epstein (aliteralmind __DASH__ github __AT__ yahoo __DOT__ com)
    
   This software is dual-licensed under the:
   - Lesser General Public License (LGPL) version 3.0 or, at your option, any later version;
   - Apache Software License (ASL) version 2.0.
    
   Either license may be applied at your discretion. More information may be found at
   - http://en.wikipedia.org/wiki/Multi-licensing.
    
   The text of both licenses is available in the root directory of this project, under the names "LICENSE_lgpl-3.0.txt" and "LICENSE_asl-2.0.txt". The latest copies may be downloaded at:
   - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
   - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
\*license*/

import java.util.Arrays;

public class Main {
    /**
       <p>Is the char in the char-array?.</p>
        
     * @return  <code>({@link #getFoundIdx(char, char[]) getFoundIdx}(to_find, to_search) &gt; -1)</code>
     */
    public static final boolean isIn(char to_find, char[] to_search) {
        return (getFoundIdx(to_find, to_search) > -1);
    }

    /**
       <p>Is the char in the char-array?.</p>
        
     * @return  <code>({@link #getFoundIdx(char, char[], boolean) getFoundIdx}(to_find, to_search, do_orderAsc) &gt; -1)</code>
     */
    public static final boolean isIn(char to_find, char[] to_search, boolean do_orderAsc) {
        return (getFoundIdx(to_find, to_search, do_orderAsc) > -1);
    }

    /**
       <p>Get the (first) index at which a char exists in a char-array.</p>
        
     * @return  {@link #getFoundIdx(char, char[], boolean) getFoundIdx(to_find, to_search, true)}
     */
    public static final int getFoundIdx(char to_find, char[] to_search) {
        return getFoundIdx(to_find, to_search, true);
    }

    /**
       <p>Get the (first) index at which a char exists in a char-array.</p>
        
     * @param  to_find  The char to analyze.
     * @param  to_search  The array which {@code to_find} should be in. May not be null, and, if {@code do_orderAsc} is true, <i>should</i> be non-empty, unique, and sorted ascending. If not, this function will not work properly.
     * @param  do_orderAsc  If true, then <code>{@link java.util.Arrays}.{@link java.util.Arrays#binarySearch(char[], char) binarySearch}(c[],c)</code> is used to search the array. If false, a for loop is used.
     * @return  The first index in {@code to_search} at which {@code to_find} exists.
       <br/>{@code <b>-1</b>} If it doesn't.
     */
    public static final int getFoundIdx(char to_find, char[] to_search, boolean do_orderAsc) {
        if (to_search == null) {
            throw new NullPointerException("to_search");
        }

        if (do_orderAsc) {
            return Arrays.binarySearch(to_search, to_find);
        }

        for (int i = 0; i < to_search.length; i++) {
            if (to_find == to_search[i]) {
                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)
  5. indexOf(T[] array, T toSearch)
  6. isIn(String name, String[] array)
  7. isIn(String name, String[] array)
  8. isInArray(final T ch, final T[] a)
  9. isInArray(String needle, String[] haystack)