Guava教程
Chars是基本char类型的实用工具类
类声明
以下是com.google.common.primitives.Chars类的声明:
@GwtCompatible(emulated=true)
public final class Chars
extends Object
字段
方法
方法继承
这个类从以下类继承的方法: 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
转载自并发编程网-ifeve.com