当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Guava Ints contains()用法及代码示例


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

用法:

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

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


  • array:一个int值数组,可能为空。
  • target:基本的int值。

返回值:此方法返回一个布尔值。如果array [i] ==目标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

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



相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Ints contains() function | Guava | Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。