Guava Ints – contains() 函数

来源:这里教程网 时间:2026-02-17 21:41:46 作者:

Guava Ints contains() 函数

Guava的Ints.contains()如果目标在数组的任何地方作为一个元素存在,则返回true.

语法:

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

参数: 该方法接受以下参数:

array: 一个int值的数组,可能是空的。target: 一个原始的int值。

返回值:该方法返回一个布尔值。如果array[i] == target for some value of i,它返回True.

示例1:

// Java code to show implementation of// Guava's Ints.contains() method import com.google.common.primitives.Ints;import java.util.Arrays; class GFG {     // Driver's code    public static void main(String[] args)    {         // Creating an Integer array        int[] arr = { 5, 4, 3, 2, 1 };         int target = 3;         // Using Ints.contains() method to search        // for an element in the array. The method        // returns true if element is found, else        // returns false        if (Ints.contains(arr, target))            System.out.println("Target is present"                               + " in the array");        else            System.out.println("Target is not present"                               + " in the array");    }}

输出:

Target is present in the array

示例2:

// Java code to show implementation of// Guava's Ints.contains() method import com.google.common.primitives.Ints;import java.util.Arrays; class GFG {     // Driver's code    public static void main(String[] args)    {         // Creating an Integer array        int[] arr = { 2, 4, 6, 8, 10 };         int target = 7;         // Using Ints.contains() method to search        // for an element in the array. The method        // returns true if element is found, else        // returns false        if (Ints.contains(arr, target))            System.out.println("Target is present"                               + " in the array");        else            System.out.println("Target is not present"                               + " in the array");    }}

输出:

Target is not present in the array

Reference: https://google.github.io/guava/releases/22.0/api/docs/com/google/common/primitives/Ints.html#contains-int:A-int-

相关推荐