当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。