/** * <p>Checks if the object is in the given array. * * <p>The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param objectToFind the object to find * @return {@code true} if the array contains the object */ public static boolean contains(final Object[] array, final Object objectToFind) { return indexOf(array, objectToFind) != INDEX_NOT_FOUND; }
/** * <p>Checks if the value is in the given array. * * <p>The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param valueToFind the value to find * @return {@code true} if the array contains the object */ public static boolean contains(final boolean[] array, final boolean valueToFind) { return indexOf(array, valueToFind) != INDEX_NOT_FOUND; }
/** * <p>Checks if the value is in the given array. * * <p>The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param valueToFind the value to find * @return {@code true} if the array contains the object */ public static boolean contains(final short[] array, final short valueToFind) { return indexOf(array, valueToFind) != INDEX_NOT_FOUND; }
/** * <p>Checks if the value is in the given array. * * <p>The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param valueToFind the value to find * @return {@code true} if the array contains the object */ public static boolean contains(final long[] array, final long valueToFind) { return indexOf(array, valueToFind) != INDEX_NOT_FOUND; }
/** * <p>Checks if the value is in the given array. * * <p>The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param valueToFind the value to find * @return {@code true} if the array contains the object */ public static boolean contains(final float[] array, final float valueToFind) { return indexOf(array, valueToFind) != INDEX_NOT_FOUND; }
/** * <p>Checks if the value is in the given array. * * <p>The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param valueToFind the value to find * @return {@code true} if the array contains the object */ public static boolean contains(final int[] array, final int valueToFind) { return indexOf(array, valueToFind) != INDEX_NOT_FOUND; }
/** * <p>Checks if the value is in the given array. * * <p>The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param valueToFind the value to find * @return {@code true} if the array contains the object */ public static boolean contains(final byte[] array, final byte valueToFind) { return indexOf(array, valueToFind) != INDEX_NOT_FOUND; }
/** * <p>Checks if the value is in the given array. * * <p>The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param valueToFind the value to find * @return {@code true} if the array contains the object */ public static boolean contains(final double[] array, final double valueToFind) { return indexOf(array, valueToFind) != INDEX_NOT_FOUND; }
/** * <p>Finds the index of the given value in the array. * * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * * @param array the array to search through for the object, may be {@code null} * @param valueToFind the value to find * @return the index of the value within the array, * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input */ public static int indexOf(final long[] array, final long valueToFind) { return indexOf(array, valueToFind, 0); }
/** * <p>Finds the index of the given value in the array. * * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * * @param array the array to search through for the object, may be {@code null} * @param valueToFind the value to find * @return the index of the value within the array, * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input */ public static int indexOf(final float[] array, final float valueToFind) { return indexOf(array, valueToFind, 0); }
/** * <p>Finds the index of the given object in the array. * * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * * @param array the array to search through for the object, may be {@code null} * @param objectToFind the object to find, may be {@code null} * @return the index of the object within the array, * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input */ public static int indexOf(final Object[] array, final Object objectToFind) { return indexOf(array, objectToFind, 0); }
/** * <p>Finds the index of the given value in the array. * * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * * @param array the array to search through for the object, may be {@code null} * @param valueToFind the value to find * @return the index of the value within the array, * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input */ public static int indexOf(final int[] array, final int valueToFind) { return indexOf(array, valueToFind, 0); }
/** * <p>Finds the index of the given value in the array. * * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * * @param array the array to search through for the object, may be {@code null} * @param valueToFind the value to find * @return the index of the value within the array, * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input */ public static int indexOf(final short[] array, final short valueToFind) { return indexOf(array, valueToFind, 0); }
/** * <p>Checks if the value is in the given array. * * <p>The method returns {@code false} if a {@code null} array is passed in. * * @param array the array to search through * @param valueToFind the value to find * @return {@code true} if the array contains the object * @since 2.1 */ public static boolean contains(final char[] array, final char valueToFind) { return indexOf(array, valueToFind) != INDEX_NOT_FOUND; }
@Test public void testIndexOfByte() { byte[] array = null; assertEquals(-1, ArrayUtils.indexOf(array, (byte) 0)); array = new byte[]{0, 1, 2, 3, 0}; assertEquals(0, ArrayUtils.indexOf(array, (byte) 0)); assertEquals(1, ArrayUtils.indexOf(array, (byte) 1)); assertEquals(2, ArrayUtils.indexOf(array, (byte) 2)); assertEquals(3, ArrayUtils.indexOf(array, (byte) 3)); assertEquals(-1, ArrayUtils.indexOf(array, (byte) 99)); }
@Test public void testIndexOfLong() { long[] array = null; assertEquals(-1, ArrayUtils.indexOf(array, 0)); array = new long[]{0, 1, 2, 3, 0}; assertEquals(0, ArrayUtils.indexOf(array, 0)); assertEquals(1, ArrayUtils.indexOf(array, 1)); assertEquals(2, ArrayUtils.indexOf(array, 2)); assertEquals(3, ArrayUtils.indexOf(array, 3)); assertEquals(-1, ArrayUtils.indexOf(array, 99)); }
@Test public void testIndexOfShort() { short[] array = null; assertEquals(-1, ArrayUtils.indexOf(array, (short) 0)); array = new short[]{0, 1, 2, 3, 0}; assertEquals(0, ArrayUtils.indexOf(array, (short) 0)); assertEquals(1, ArrayUtils.indexOf(array, (short) 1)); assertEquals(2, ArrayUtils.indexOf(array, (short) 2)); assertEquals(3, ArrayUtils.indexOf(array, (short) 3)); assertEquals(-1, ArrayUtils.indexOf(array, (short) 99)); }
@Test public void testIndexOfChar() { char[] array = null; assertEquals(-1, ArrayUtils.indexOf(array, 'a')); array = new char[]{'a', 'b', 'c', 'd', 'a'}; assertEquals(0, ArrayUtils.indexOf(array, 'a')); assertEquals(1, ArrayUtils.indexOf(array, 'b')); assertEquals(2, ArrayUtils.indexOf(array, 'c')); assertEquals(3, ArrayUtils.indexOf(array, 'd')); assertEquals(-1, ArrayUtils.indexOf(array, 'e')); }
@Test public void testIndexOfInt() { int[] array = null; assertEquals(-1, ArrayUtils.indexOf(array, 0)); array = new int[]{0, 1, 2, 3, 0}; assertEquals(0, ArrayUtils.indexOf(array, 0)); assertEquals(1, ArrayUtils.indexOf(array, 1)); assertEquals(2, ArrayUtils.indexOf(array, 2)); assertEquals(3, ArrayUtils.indexOf(array, 3)); assertEquals(-1, ArrayUtils.indexOf(array, 99)); }
@Test public void testIndexOfBoolean() { boolean[] array = null; assertEquals(-1, ArrayUtils.indexOf(array, true)); array = new boolean[0]; assertEquals(-1, ArrayUtils.indexOf(array, true)); array = new boolean[]{true, false, true}; assertEquals(0, ArrayUtils.indexOf(array, true)); assertEquals(1, ArrayUtils.indexOf(array, false)); array = new boolean[]{true, true}; assertEquals(-1, ArrayUtils.indexOf(array, false)); }