Shorts是基本類型short的實(shí)用工具類。
以下是com.google.common.primitives.Shorts類的聲明:
@GwtCompatible
public final class Shorts
? ?extends Object
字段
方法
繼承的方法
這個(gè)類繼承了以下類方法: java.lang.Object
使用所選擇的編輯器創(chuàng)建下面的java程序 C:/> Guava
GuavaTester.java
import java.util.List;
import com.google.common.primitives.Shorts;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testShorts();
}
private void testShorts(){
short[] shortArray = {1,2,3,4,5,6,7,8,9};
//convert array of primitives to array of objects
List<Short> objectArray = Shorts.asList(shortArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
shortArray = Shorts.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< shortArray.length ; i++){
System.out.print(shortArray[i] + " ");
}
System.out.println("]");
short data = 5;
//check if element is present in the list of primitives or not
System.out.println("5 is in list? "+ Shorts.contains(shortArray, data));
//Returns the minimum
System.out.println("Min: " + Shorts.min(shortArray));
//Returns the maximum
System.out.println("Max: " + Shorts.max(shortArray));
data = 2400;
//get the byte array from an integer
byte[] byteArray = Shorts.toByteArray(data);
for(int i = 0; i< byteArray.length ; i++){
System.out.print(byteArray[i] + " ");
}
}
}
驗(yàn)證結(jié)果
使用javac編譯器編譯如下類
C:\Guava>javac GuavaTester.java
現(xiàn)在運(yùn)行GuavaTester看到的結(jié)果
C:\Guava>java GuavaTester
看到結(jié)果
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[ 1 2 3 4 5 6 7 8 9 ]
5 is in list? true
Min: 1
Max: 9
9 96
轉(zhuǎn)載自并發(fā)編程網(wǎng)-ifeve.com