Ints contains() function in Java


The contains() function of the Ints class is used to check whether an element is present in the array or not.

Following is the syntax −

public static boolean
contains(int[] arr, int target)

Here, arr is the array wherein the element is to be checked. The target is the element to be checked.

Following is an example to implement the contains() method of the Ints class −

Example

import com.google.common.primitives.Ints;
import java.util.*;
class Demo {
   public static void main(String[] args) {
      int[] myArr1 = { 100, 150, 230, 300, 400 };
      int[] myArr2 = { 450, 550, 700, 800, 1000 };
      System.out.println("Array 1 = ");
      for(int i=0; i < myArr1.length; i++) {
         System.out.println(myArr1[i]);
      }
      System.out.println("Array 2 = ");
      for(int i=0; i < myArr2.length; i++) {
         System.out.println(myArr2[i]);
      }
      int[] arr = Ints.concat(myArr1, myArr2);
      System.out.println("Concatenated arrays = "+Arrays.toString(arr));
      if (Ints.contains(arr, 800))
         System.out.println("Element 800 is in the array!");
      else
         System.out.println("Element 800 is not in the array!");
   }
}

Output

Array 1 =
100
150
230
300
400
Array 2 =
450
550
700
800
1000
Concatenated arrays = [100, 150, 230, 300, 400, 450, 550, 700, 800, 1000]
Element 800 is in the array!

Updated on: 23-Sep-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements