Ints indexOf() function in Java


The indexOf() method of the Ints class returns the index of the first appearance of the value target in array. The syntax is as follows −

public static int indexOf(int[] arr, int target)

Here, parameter arr is an array of int values and target is the value to be checked for first appearance.

Let us now see an example −

Example

import com.google.common.primitives.Ints;
class Demo {
   public static void main(String[] args) {
      int[] arr = { 10, 20, 30, 40, 50, 60,70, 80, 90, 100 };
      int index = Ints.indexOf(arr, 40);
      if (index != -1) {
         System.out.println("Element 40 is in the array at position " + (index+1));
      } else {
         System.out.println("Element 40 is not in the array");
      }
   }
}

Output

Element 40 is in the array at position 4

Updated on: 24-Sep-2019

847 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements