📌  相关文章
📜  Java番石榴 |带有示例的 Longs.contains() 方法

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

Java番石榴 |带有示例的 Longs.contains() 方法

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

句法:

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

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

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

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

异常:该方法不会抛出任何异常。

示例 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-