Guava Chars类

来源:这里教程网 时间:2026-02-17 21:41:48 作者:

Guava Chars类

Chars 是原始类型char的一个实用类。它提供了与char基元有关的静态实用方法,这些方法在Character或Arrays中都没有找到。这个类中的所有操作都是严格按照数字处理的,也就是说,它们既不认识Unicode,也不依赖本地。

Guava Chars类声明 :

@GwtCompatible(emulated=true)public final class Charsextends Object

下表显示了Guava Chars类的字段摘要 :

Guava Chars类提供的一些方法是 。


异常 :

checkedCast : 如果值大于Character.MAX_VALUE或小于Character.MIN_VALUEmin : 如果数组是空的,IllegalArgumentException。max : 如果数组为空,则出现IllegalArgumentException。fromByteArray : 如果字节的元素少于2个,则出现IllegalArgumentException。ensureCapacity : 如果minLength或padding为负数,则出现IllegalArgumentException。toArray : 如果集合或其任何元素为空,则出现NullPointerException。

下表显示了Guava Chars类提供的其他一些方法:

下面给出了一些例子,显示了Guava Chars类方法的实现:
例子1:

// Java code to show implementation// of Guava Chars.asList() method  import com.google.common.primitives.Chars;import java.util.*;  class GFG {    // Driver method    public static void main(String[] args)    {        char arr[] = { 'g', 'e', 'e', 'k', 's' };          // Using Chars.asList() method which        // converts array of primitives        // to array of objects        List<Character> myList = Chars.asList(arr);          // Displaying the elements        System.out.println(myList);    }}

输出:

[g, e, e, k, s]

例子2:

// Java code to show implementation// of Guava Chars.toArray() method  import com.google.common.primitives.Chars;import java.util.*;  class GFG {    // Driver method    public static void main(String[] args)    {        List<Character> myList = Arrays.asList('g', 'e', 'e', 'k', 's');          // Using Chars.toArray() method which        // converts a List of Chars to an        // array of char        char[] arr = Chars.toArray(myList);          // Displaying the elements        System.out.println(Arrays.toString(arr));    }}

输出:

[g, e, e, k, s]

例子3:

// Java code to show implementation// of Guava Chars.concat() method  import com.google.common.primitives.Chars;import java.util.*;  class GFG {    // Driver method    public static void main(String[] args)    {        char[] arr1 = { 'g', 'e', 'e' };        char[] arr2 = { 'k', 's' };          // Using Chars.concat() method which        // combines arrays from specified        // arrays into a single array        char[] arr = Chars.concat(arr1, arr2);          // Displaying the elements        System.out.println(Arrays.toString(arr));    }}

输出:

[g, e, e, k, s]

例子4:

// Java code to show implementation// of Guava Chars.contains() method  import com.google.common.primitives.Chars;  class GFG {    // Driver method    public static void main(String[] args)    {        char[] arr = { 'g', 'e', 'e', 'k', 's' };          // Using Chars.contains() method which        // checks if element is present in array        // or not        System.out.println(Chars.contains(arr, 'g'));        System.out.println(Chars.contains(arr, 'm'));    }}

输出:

truefalse

例子5:

// Java code to show implementation// of Guava Chars.min() method  import com.google.common.primitives.Chars;  class GFG {    // Driver method    public static void main(String[] args)    {        char[] arr = { 'g', 'e', 'e', 'k', 's' };          // Using Chars.min() method        System.out.println(Chars.min(arr));    }}

输出:

e

例子6:

// Java code to show implementation// of Guava Chars.max() method  import com.google.common.primitives.Chars;  class GFG {    // Driver method    public static void main(String[] args)    {        char[] arr = { 'g', 'e', 'e', 'k', 's' };          // Using Chars.max() method        System.out.println(Chars.max(arr));    }}

输出:

s

相关推荐