Java中的Ints contains()函数

contains()Ints类的函数用于检查数组中是否存在元素。

以下是语法-

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

在此,arr是要检查元素的数组。目标是要检查的元素。

以下是实现contains()Ints类的方法的示例-

示例

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("元素800在数组中!");
      else
         System.out.println("元素800不在数组中!");
   }
}

输出结果

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]
元素800在数组中!