Guava Chars类

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

Guava Chars类

Chars是一个用于原始类型char的实用类。

类声明

以下是声明的内容 com.google.common.primitives.Chars 类−

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

字段

序号字段和描述
1static int BYTES 表示原始字符值所需的字节数。

方法

序号方法与描述
1static List asList(char… backingArray) 返回由指定数组支持的固定大小列表,类似于Arrays.asList(Object[])。
2static char checkedCast(long value) 返回与value相等的char值(如果可能)。
3static int compare(char a, char b) 比较两个指定的char值。
4static char[] concat(char[]… arrays) 将每个提供的数组的值组合成一个单一的数组返回。
5static boolean contains(char[] array, char target) 如果目标在数组中的任何位置作为一个元素存在,则返回true。
6static char[] ensureCapacity(char[] array, int minLength, int padding) 返回一个包含与数组相同值的数组,但保证至少具有指定的最小长度。
7static char fromByteArray(byte[] bytes) 返回以bytes的前两个字节存储的大端表示形式的char值;等同于ByteBuffer.wrap(bytes).getChar()。
8static char fromBytes(byte b1, byte b2) 返回以给定的两个字节的大端顺序表示的char值,等同于Chars.fromByteArray(new byte[] {b1, b2})。
9static int hashCode(char value) 返回值的哈希码;等同于调用 ((Character) value).hashCode() 的结果。
10static int indexOf(char[] array, char target) 返回数组中值 target 第一次出现的索引。
11static int indexOf(char[] array, char[] target) 返回数组中指定目标第一次出现的起始位置,如果没有出现,则返回 -1。
12static String join(String separator, char… array) 返回一个包含用分隔符分隔的 char 值的字符串。
13static int lastIndexOf(char[] array, char target) 返回数组中值目标的最后出现的索引。
14static Comparator <char[]> lexicographicalComparator() 返回一个按字典顺序比较两个char数组的比较器。
15static char max(char… array) 返回数组中的最大值。
16static char min(char… array) 返回数组中的最小值。
17static char saturatedCast(long value) 返回与值最接近的char值。
18static char[] toArray(Collection collection)返回由集合中的元素组成的char数组。将一组Character实例复制到新的原始char值数组中。
19static 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? truec position in list 2Min: aMax: h

相关推荐