Guava - Chars 類



Chars 是一個用於基本型別 char 的工具類。

類宣告

以下是 com.google.common.primitives.Chars 類的宣告:

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

欄位

序號 欄位及描述
1

static int BYTES

表示基本型別 char 值所需的位元組數。

方法

序號 方法及描述
1

static List<Character> asList(char... backingArray)

返回一個由指定陣列支援的固定大小列表,類似於 Arrays.asList(Object[])。

2

static char checkedCast(long value)

如果可能,返回等於 value 的 char 值。

3

static int compare(char a, char b)

比較兩個指定的 char 值。

4

static char[] concat(char[]... arrays)

將每個提供的陣列中的值組合到一個數組中。

5

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

如果 target 在陣列中的任何位置存在於陣列中,則返回 true。

6

static char[] ensureCapacity(char[] array, int minLength, int padding)

返回一個包含與陣列相同值的陣列,但保證具有指定的最小長度。

7

static char fromByteArray(byte[] bytes)

返回其大端表示儲存在 bytes 的前 2 個位元組中的 char 值;等效於 ByteBuffer.wrap(bytes).getChar()。

8

static char fromBytes(byte b1, byte b2)

返回其位元組表示為給定的 2 個位元組的 char 值,以大端順序;等效於 Chars.fromByteArray(new byte[] {b1, b2})。

9

static int hashCode(char value)

返回 value 的雜湊碼;等效於呼叫 ((Character) value).hashCode() 的結果。

10

static int indexOf(char[] array, char target)

返回 target 在陣列中第一次出現的位置。

11

static int indexOf(char[] array, char[] target)

返回陣列中指定目標的第一次出現開始位置,如果不存在則返回 -1。

12

static String join(String separator, char... array)

返回一個包含由 separator 分隔的提供的 char 值的字串。

13

static int lastIndexOf(char[] array, char target)

返回 target 在陣列中最後一次出現的位置。

14

static Comparator<char[]> lexicographicalComparator()

返回一個按字典順序比較兩個 char 陣列的比較器。

15

static char max(char... array)

返回陣列中最大的值。

16

static char min(char... array)

返回陣列中最小的值。

17

static char saturatedCast(long value)

返回最接近 value 的 char 值。

18

static char[] toArray(Collection<Character> collection)

將 Character 例項的集合複製到一個新的基本型別 char 值陣列中。

19

static byte[] toByteArray(char value)

返回 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
guava_primitive_utilities.htm
廣告

© . All rights reserved.