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


Java Guava Ints contains()用法及代碼示例


如果目標在數組中的任何位置作為元素存在,則 Guava 的Ints.contains()返回true。

用法:

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

參數:此方法接受以下參數:


  • array:一個int值數組,可能為空。
  • target:基本的int值。

返回值:此方法返回一個布爾值。如果array [i] ==目標i的某個值,則返回True。

範例1:

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



相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Ints contains() function | Guava | Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。