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


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


Shorts.contains()是Guava库中Shorts类的一种方法,用于检查值目标是否作为数组中任何位置的元素存在。目标值和数组均作为该方法的参数。它返回一个布尔值,说明是否存在目标值。

用法:

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

参数:此方法采用两个强制性参数:


  • array:这是一个短值数组,在其中要搜索目标。也可以为空。
  • target:这是必须在数组中搜索的原始short值。

返回值:此方法返回一个布尔值,该值说明目标值是否存在于数组中。如果存在目标,则返回True。

以下示例程序旨在说明上述方法的使用:

示例1:

// Java code to show implementation of 
// Guava's Shorts.contains() method 
import com.google.common.primitives.Shorts; 
import java.util.Arrays; 
  
class GFG { 
    // Driver's code 
    public static void main(String[] args) 
    { 
        // Creating a short array 
        short[] arr = { 5, 4, 3, 2, 1 }; 
  
        short target = 3; 
  
        // Using Shorts.contains() method to search 
        // for an element in the array. The method 
        // returns true if element is found, else 
        // returns false 
        if (Shorts.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 Shorts.contains() method 
import com.google.common.primitives.Shorts; 
import java.util.Arrays; 
  
class GFG { 
    // Driver's code 
    public static void main(String[] args) 
    { 
        // Creating a short array 
        short[] arr = { 2, 4, 6, 8, 10 }; 
  
        short target = 7; 
  
        // Using Shorts.contains() method to search 
        // for an element in the array. The method 
        // returns true if element is found, else 
        // returns false 
        if (Shorts.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/23.0/api/docs/com/google/common/primitives/Shorts.html#contains-short:A-short-



相关用法


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