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


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