📌  相关文章
📜  Java番石榴 |带有示例的 Bytes.contains() 方法

📅  最后修改于: 2022-05-13 01:54:41.282000             🧑  作者: Mango

Java番石榴 |带有示例的 Bytes.contains() 方法

Guava 的 Bytes 类的Bytes.contains()方法接受两个参数arraytarget 。该方法用于检查目标元素是否存在于数组中。

句法:

public static boolean contains(byte[] array,
                               byte 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)