Here you can find the source of indexOf(T[] array, T toSearch)
Parameter | Description |
---|---|
array | The array to search in. |
toSearch | The element to search. |
public static <T> int indexOf(T[] array, T toSearch)
//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; } } }