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


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


Guava的Longs Class的Longs.contains()方法用于检查数组中是否存在值(即目标)。该目标值和数组作为该方法的参数。并且此方法返回一个布尔值,该布尔值说明目标值是否为

用法:

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

参数:此方法接受两个参数:


  • array:这是要在其中搜索目标值的值的数组
  • target:这是要检查是否存在的long值。

返回值:如果对于i的某个值,在i处存在的值,则该方法返回Trueth索引等于目标,否则返回

异常:该方法不会引发任何异常。

范例1:

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



相关用法


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