Java Array Has inArray(T needle, T[] hayshack)

Here you can find the source of inArray(T needle, T[] hayshack)

Description

Tell whether an object is in an array

License

Open Source License

Parameter

Parameter Description
T a parameter
needle Object to look for
hayshack The array to look from

Return

True if found, false otherwise.

Declaration

public static <T> boolean inArray(T needle, T[] hayshack) 

Method Source Code

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

public class Main {
    /**//from   w  w  w  . j  ava2 s . c  om
     * Tell whether an object is in an array
     * @param <T>
     * @param needle Object to look for
     * @param hayshack The array to look from
     * @return True if found, false otherwise.
     */
    public static <T> boolean inArray(T needle, T[] hayshack) {
        for (int i = 0; i < hayshack.length; ++i) {
            if (needle.equals(hayshack[i])) {
                return true;
            }
        }
        return false;
    }

    /**
     * Tell whether an int is in an array of ints
     * @param needle int to find
     * @param hayshack int list to find from
     * @return true if the int is in the list, false otherwise
     */
    public static boolean inArray(int needle, int[] hayshack) {
        for (int i = 0; i < hayshack.length; ++i) {
            if (needle == hayshack[i]) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. inArray(String value, String[] values)
  2. inArray(String[] attributes, String attribute)
  3. inArray(String[] delegableOperations, String operationId)
  4. inArray(String[] haystack, String needle)
  5. inArray(T el, T[] array)
  6. inArray(T[] array, T element)