- Guava 教程
- Guava - 首页
- Guava - 概述
- Guava - 环境设置
- Guava - Optional 类
- Guava - Preconditions 类
- Guava - Ordering 类
- Guava - Objects 类
- Guava - Range 类
- Guava - Throwables 类
- Guava - 集合工具类
- Guava - 缓存工具类
- Guava - 字符串工具类
- Guava - 基本类型工具类
- Guava - 数学工具类
- Guava 有用资源
- Guava - 快速指南
- Guava - 有用资源
- Guava - 讨论
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