Guava – Chars.contains()方法与实例

Guava Chars.contains()方法与实例

Guava库中Chars类的contains()方法是用来检查指定的值是否存在于指定的char值数组中。要搜索的char值和要搜索的char数组都被作为一个参数.

语法:

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

参数: 该方法接受两个强制性参数。

  • array: 这是一个char值数组,目标值将在其中被搜索。
  • target: 它是要在数组中搜索的char值。

返回值: 该方法返回一个布尔值,说明目标字符值是否存在于指定的字符阵列中。如果目标值存在于数组中,它返回True。否则,它返回False。

以下程序说明了contains()方法的使用。

示例1:

// Java code to show implementation of
// Guava's Chars.contains() method
  
import com.google.common.primitives.Chars;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a character array
        char[] arr = { 'g', 'e', 'e', 'k', 's' };
  
        char target = 'k';
  
        // Using Chars.contains() method to search
        // for an element in the array. The method
        // returns true if element is found, else
        // returns false
        if (Chars.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 Chars.contains() method
  
import com.google.common.primitives.Chars;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a character array
        char[] arr = { 'g', 'e', 'e', 'k', 's' };
  
        char target = 'a';
  
        // Using Chars.contains() method to search
        // for an element in the array. The method
        // returns true if element is found, else
        // returns false
        if (Chars.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/18.0/api/docs/com/google/common/primitives/Chars.html#contains(char[], %20char)](https://google.github.io/guava/releases/18.0/api/docs/com/google/common/primitives/Chars.html#contains(char[], char))

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程