📌  相关文章
📜  Ints contains()函数|番石榴 |Java

📅  最后修改于: 2022-05-13 01:55:12.205000             🧑  作者: Mango

Ints contains()函数|番石榴 |Java

如果目标作为元素存在于数组中的任何位置,Guava 的Ints .contains()将返回true
句法:

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

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

  • 数组:一个 int 值数组,可能为空。
  • 目标:一个原始的 int 值。

返回值:此方法返回一个布尔值。如果 array[i] == target 对于 i 的某个值,它返回True
示例 1:

Java
// 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");
    }
}


Java
// 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 present in the array

示例 2:

Java

// 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

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