Java Array Index Of indexOf(Object[] array, Object objectToFind)

Here you can find the source of indexOf(Object[] array, Object objectToFind)

Description

Finds the index of the given object in the array.

This method returns #INDEX_NOT_FOUND ( -1 ) for a null input array.

License

Apache License

Parameter

Parameter Description
array the array to search through for the object, may be null
objectToFind the object to find, may be null

Return

the index of the object within the array, ( -1 ) if not found or null array input

Declaration

private static int indexOf(Object[] array, Object objectToFind) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//ww  w . j  av  a2s  .c  o  m
     * The index value when an element is not found in a list or array: {@code -1}.
     * This value is returned by methods in this class and can also be used in comparisons with values returned by
     * various method from {@link java.util.List}.
     */
    private static final int INDEX_NOT_FOUND = -1;

    /**
     * <p>Finds the index of the given object in the array.</p>
     *
     * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.</p>
     *
     * @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
     */
    private static int indexOf(Object[] array, Object objectToFind) {
        return indexOf(array, objectToFind, 0);
    }

    /**
     * <p>Finds the index of the given object in the array starting at the given index.</p>
     *
     * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.</p>
     *
     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
     * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).</p>
     *
     * @param array  the array to search through for the object, may be {@code null}
     * @param objectToFind  the object to find, may be {@code null}
     * @param startIndex  the index to start searching at
     * @return the index of the object within the array starting at the index,
     *  {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
     */
    private static int indexOf(Object[] array, Object objectToFind, int startIndex) {
        if (array == null) {
            return INDEX_NOT_FOUND;
        }
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (objectToFind == null) {
            for (int i = startIndex; i < array.length; i++) {
                if (array[i] == null) {
                    return i;
                }
            }
        } else if (array.getClass().getComponentType().isInstance(objectToFind)) {
            for (int i = startIndex; i < array.length; i++) {
                if (objectToFind.equals(array[i])) {
                    return i;
                }
            }
        }
        return INDEX_NOT_FOUND;
    }
}

Related

  1. indexOf(char toBeFound, char[] array)
  2. indexOf(final String[] p_elements, final String p_key)
  3. indexOf(final T[] array, final T value)
  4. indexOf(int[] arr, int e)
  5. indexOf(int[] arr, int val)
  6. indexOf(Object[] array, Object objectToFind)
  7. indexOf(Object[] elements, Object value)
  8. indexOfCS(String[] strings, String a)
  9. indexOfIdentical(Object[] array, Object value)