Java Array Search indexOf(T[] array, T toSearch)

Here you can find the source of indexOf(T[] array, T toSearch)

Description

Returns the index of the element to search in the given array.

License

Open Source License

Parameter

Parameter Description
array The array to search in.
toSearch The element to search.

Return

The index of the element or -1 if it was not found.

Declaration

public static <T> int indexOf(T[] array, T toSearch) 

Method Source Code

//package com.java2s;
// Public License. See LICENSE.TXT for details.

import java.util.Iterator;

public class Main {
    /**/*  ww  w  .  j av a  2s.c  o  m*/
     * Returns the index of the element to search in the given iterator.
     * @param iter The iterator to search in.
     * @param toSearch The element to search.
     * @return The index of the element or {@code -1} if it was not found.
     */
    public static <T> int indexOf(Iterator<T> iter, T toSearch) {
        if (iter != null) {
            int i = 0;
            boolean found = false;
            while (!found && iter.hasNext()) {
                T next = iter.next();
                if (next != null ? next.equals(toSearch) : toSearch == null) {
                    found = true;
                } else {
                    i++;
                }
            }
            if (found) {
                return i;
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }

    /**
     * Returns the index of the element to search in the given array.
     * @param array The array to search in.
     * @param toSearch The element to search.
     * @return The index of the element or {@code -1} if it was not found.
     */
    public static <T> int indexOf(T[] array, T toSearch) {
        int index = -1;
        if (array != null) {
            int i = 0;
            while (i < array.length && index < 0) {
                if (array[i] != null ? array[i].equals(toSearch) : toSearch == null) {
                    index = i;
                }
                i++;
            }
        }
        return index;
    }

    /**
     * Nullpointer save execution of {@link Object#equals(Object)}.
     * The two objects are also equal if both references are {@code null}
     * @param first The first {@link Object}.
     * @param second The second {@link Object}.
     * @return {@code true} objects are equal or both {@code null}, {@code false} otherwise
     */
    public static boolean equals(Object first, Object second) {
        if (first != null) {
            if (second != null) {
                return first.equals(second);
            } else {
                return false;
            }
        } else {
            return second == null;
        }
    }
}

Related

  1. arraySearch(byte[] array, byte[] search)
  2. arraySearch(int[] arr, int i)
  3. byteArraySearch(byte[] data, byte[] searchData)
  4. contains(byte[] array, byte toSearch)
  5. getFoundIdx(char to_find, char[] to_search, boolean do_orderAsc)
  6. isIn(char to_find, char[] to_search)
  7. isIn(String name, String[] array)
  8. isIn(String name, String[] array)
  9. isInArray(final T ch, final T[] a)