Java Array Contain arrayContains(final E[] arr, final E targetValue)

Here you can find the source of arrayContains(final E[] arr, final E targetValue)

Description

Check if an array contains a value, <a href="http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/">very efficient</a>

License

Open Source License

Parameter

Parameter Description
arr The array to check against
targetValue The value to check for

Return

true if the value is found, false otherwise

Declaration

public static <E> boolean arrayContains(final E[] arr, final E targetValue) 

Method Source Code

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

public class Main {
    /**//  w ww.ja va 2  s .c  om
     * Check if an array contains a value, <a href="http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/">very efficient</a>
     *
     * @param arr         The array to check against
     * @param targetValue The value to check for
     * @return {@code true} if the value is found, {@code false} otherwise
     */
    public static <E> boolean arrayContains(final E[] arr, final E targetValue) {
        for (final E s : arr) {
            if (s.equals(targetValue))
                return true;
        }
        return false;
    }

    /**
     * Check if an array contains a value, <a href="http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/">very efficient</a>
     *
     * @param arr         The array to check against
     * @param targetValue The value to check for
     * @return {@code true} if the value is found, {@code false} otherwise
     */
    static boolean arrayContains(final int[] arr, final int targetValue) {
        for (final int s : arr) {
            if (s == targetValue)
                return true;
        }
        return false;
    }

    /**
     * Check if an array contains a value, <a href="http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/">very efficient</a>
     *
     * @param arr         The array to check against
     * @param targetValue The value to check for
     * @return {@code true} if the value is found, {@code false} otherwise
     */
    public static boolean arrayContains(final byte[] arr, final byte targetValue) {
        for (final byte s : arr) {
            if (s == targetValue)
                return true;
        }
        return false;
    }

    /**
     * Check if an array contains a value, <a href="http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/">very efficient</a>
     *
     * @param arr         The array to check against
     * @param targetValue The value to check for
     * @return {@code true} if the value is found, {@code false} otherwise
     */
    public static boolean arrayContains(final short[] arr, final short targetValue) {
        for (final short s : arr) {
            if (s == targetValue)
                return true;
        }
        return false;
    }

    /**
     * Check if an array contains a value, <a href="http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/">very efficient</a>
     *
     * @param arr         The array to check against
     * @param targetValue The value to check for
     * @return {@code true} if the value is found, {@code false} otherwise
     */
    public static boolean arrayContains(final char[] arr, final char targetValue) {
        for (final char s : arr) {
            if (s == targetValue)
                return true;
        }
        return false;
    }

    /**
     * Check if an array contains a value, <a href="http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/">very efficient</a>
     *
     * @param arr         The array to check against
     * @param targetValue The value to check for
     * @return {@code true} if the value is found, {@code false} otherwise
     */
    public static boolean arrayContains(final long[] arr, final long targetValue) {
        for (final long s : arr) {
            if (s == targetValue)
                return true;
        }
        return false;
    }

    /**
     * Check if an array contains a value, <a href="http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/">very efficient</a>
     *
     * @param arr         The array to check against
     * @param targetValue The value to check for
     * @return {@code true} if the value is found, {@code false} otherwise
     */
    public static boolean arrayContains(final float[] arr, final float targetValue) {
        for (final float s : arr) {
            if (s == targetValue)
                return true;
        }
        return false;
    }

    /**
     * Check if an array contains a value, <a href="http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/">very efficient</a>
     *
     * @param arr         The array to check against
     * @param targetValue The value to check for
     * @return {@code true} if the value is found, {@code false} otherwise
     */
    public static boolean arrayContains(final double[] arr, final double targetValue) {
        for (final double s : arr) {
            if (s == targetValue)
                return true;
        }
        return false;
    }
}

Related

  1. ARRAY_Contains(int value, int[] array)
  2. arrayContains(byte[] a, int offset, byte[] b)
  3. arrayContains(char[] chars, char cToFind)
  4. arrayContains(char[] chars, char cToFind)
  5. arrayContains(final byte[] what, final byte[] b)
  6. arrayContains(final int[] array, final int v)
  7. arrayContains(final Object[] a, final Object v)
  8. arrayContains(final String[] array, String needle)
  9. arrayContains(final T[] array, final T v)