Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

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: 2019-09-24T07:10:34+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements