Guava Chars類

Chars是基本char類型的實用工具類。

類聲明

以下是com.google.common.primitives.Chars類的聲明:

@GwtCompatible(emulated=true) public final class Chars extends Object

字段

S.N.

字段及說明

1

static int BYTES
所需要的字節數來表示一個原始char值。

方法

S.N.

方法及說明

1

static List asList(char... backingArray)
返回由指定數組支持的固定大小的列表,類似 Arrays.asList(Object[]).

2

static char checkedCast(long value)
返回char值等於value值,如果可能的話。

3

static int compare(char a, char b)
比較兩個指定的char值。

4

static char[] concat(char[]... arrays)
每個數組提供組合成一個單一的數組,則返回值。

5

static boolean contains(char[] array, char target)
返回true,如果target是否存在在任何地方數組元素。

6

static char[] ensureCapacity(char[] array, int minLength, int padding)
返回一個包含相同的值數組的數組,但保證是一個規定的最小長度。

7

static char fromByteArray(byte[] bytes)
返回char值,其大端表示被存儲在第一個2字節的字節;相當於 ByteBuffer.wrap(bytes).getChar().

8

static char fromBytes(byte b1, byte b2)
返回char值的字節表示是給定2個字節,在big-endian的順序;相當於 Chars.fromByteArray(new byte[] {b1, b2}).

9

static int hashCode(char value)
返回哈希碼的值;等於調用的結果 ((Character) value).hashCode().

10

static int indexOf(char[] array, char target)
返回目標數組的首次出現的索引值。

11

static int indexOf(char[] array, char[] target)
返回指定目標的第一個匹配的起始位置數組內,或-1,如果不存在。

12

static String join(String separator, char... array)
返回包含由分離器分離所提供的char值字符串。

13

static int lastIndexOf(char[] array, char target)
返回target 在數組中最後一個出現的索引值。

14

static Comparator<char[]> lexicographicalComparator()
返回一個比較器,它比較兩個字符數組字典順序。

15

static char max(char... array)
返回在數組中的最大值。

16

static char min(char... array)
返回出現在數組最小值。

17

static char saturatedCast(long value)
返回值char最近的值。

18

static char[] toArray(Collection collection)
複製字符實例的集合到原始char值的新數組。

19

static byte[] toByteArray(char value)
返回在2元素的字節數組值大端表示;相當於 ByteBuffer.allocate(2).putChar(value).array().

方法繼承

這個類從以下類繼承的方法:

  • java.lang.Object

Chars 例子

選擇使用任何編輯器創建以下java程序在 C:/> Guava

GuavaTester.java

import java.util.List; import com.google.common.primitives.Chars; public class GuavaTester { public static void main(String args[]){ GuavaTester tester = new GuavaTester(); tester.testChars(); } private void testChars(){ char[] charArray = {'a','b','c','d','e','f','g','h'}; //convert array of primitives to array of objects List<Character> objectArray = Chars.asList(charArray); System.out.println(objectArray.toString()); //convert array of objects to array of primitives charArray = Chars.toArray(objectArray); System.out.print("[ "); for(int i = 0; i< charArray.length ; i++){ System.out.print(charArray[i] + " "); } System.out.println("]"); //check if element is present in the list of primitives or not System.out.println("c is in list? "+ Chars.contains(charArray, 'c')); //return the index of element System.out.println("c position in list "+ Chars.indexOf(charArray, 'c')); //Returns the minimum System.out.println("Min: " + Chars.min(charArray)); //Returns the maximum System.out.println("Max: " + Chars.max(charArray)); } }

驗證結果

使用javac編譯器編譯如下類

C:\Guava>javac GuavaTester.java

現在運行GuavaTester看到的結果

C:\Guava>java GuavaTester

看到結果。

[a, b, c, d, e, f, g, h]
[ a b c d e f g h ]
c is in list? true
c position in list 2
Min: a
Max: h