當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。