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


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


Guava的Bytes類的Bytes.contains()方法接受兩個參數array和target。該方法用於檢查目標元素是否存在於數組中。

用法:

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

參數:該方法接受兩個參數:


  • array:字節值數組,可能為空。
  • target:要檢查數組中是否存在原始字節值。

返回值:如果目標在數組中的任何位置作為元素存在,則該方法返回true;如果目標在數組中的任何位置都不存在,則返回false。

異常:該方法不會引發任何異常。

以下示例說明了上述方法的實現:

範例1:

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



相關用法


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