Java Array Find getIndexObject(Object[] data, Object object)

Here you can find the source of getIndexObject(Object[] data, Object object)

Description

Find the index of an element in an array.

License

Open Source License

Parameter

Parameter Description
data The array to search in
object The element to search for

Exception

Parameter Description
NullPointerException If the object to search for is null
NoSuchElementException If the specified object cannot be found

Declaration

public static int getIndexObject(Object[] data, Object object) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {
    /**/*from w  ww .j a  va 2 s.  c om*/
     * Find the index of an element in an array. Elements are compared using the
     * object.equals(otherObject) method.
     *
     * @param data The array to search in
     * @param object The element to search for
     * @returns The index of that element
     * @throws NullPointerException If the object to search for is null
     * @throws NoSuchElementException If the specified object cannot be found
     */
    public static int getIndexObject(Object[] data, Object object) {
        int x = new ArrayList<>(Arrays.asList(data)).indexOf(object);
        if (x == -1) {
            throw new NoSuchElementException("StringUtils : index lookup in array failed for object " + object
                    + ". Data length is " + data.length);
        }
        return x;
    }
}

Related

  1. findAll(int[] arr1, int[] arr2)
  2. findAllArgumentPermutations(Object[][] allArguments)
  3. findAllOrientations(int[][] matrix)
  4. getIndex(double income, String[] scopes)
  5. getIndex(String[] array, String value)
  6. getIndexOf(int i, int[] array)