Shorts是基本類型short的實用工具類。
以下是com.google.common.primitives.Shorts類的聲明:
@GwtCompatible public final class Shorts extends Object
S.N. | 字段及說明 |
---|---|
1 |
static int BYTES 所需要的字節(jié)數(shù)來表示一個原始short的值。 |
2 |
static short MAX_POWER_OF_TWO 兩個最大的冪可以被表示為short。 |
S.N. | 方法及說明 |
---|---|
1 |
static List<Short> asList(short... backingArray) 返回由指定數(shù)組支持的固定大小的列表,類似 Arrays.asList(Object[]). |
2 |
static short checkedCast(long value) 返回 short 值,該值等于value,如果可能的話。 |
3 |
static int compare(short a, short b) 比較兩個指定的short值。 |
4 |
static short[] concat(short[]... arrays) 每個陣列提供組合成一個單一的陣列,則返回其值。 |
5 |
static boolean contains(short[] array, short target) 返回true,如果目標(biāo)是否存在在任何地方數(shù)組元素。 |
6 |
static short[] ensureCapacity(short[] array, int minLength, int padding) 返回一個包含相同的值數(shù)組的數(shù)組,但保證是一個規(guī)定的最小長度。 |
7 |
static short fromByteArray(byte[] bytes) 返回短值,其大端表示被存儲在最前2字節(jié)的字節(jié);相當(dāng)于 ByteBuffer.wrap(bytes).getShort(). |
8 |
static short fromBytes(byte b1, byte b2) 返回short值的字節(jié)表示的是給定2個字節(jié),以 big-endian 順序; 相當(dāng)于 Shorts.fromByteArray(new byte[] {b1, b2}). |
9 |
static int hashCode(short value) 返回值的哈希碼;等于調(diào)用的結(jié)果 ((Short) value).hashCode(). |
10 |
static int indexOf(short[] array, short target) 返回值目標(biāo)數(shù)組的首次出現(xiàn)的索引。 |
11 |
static int indexOf(short[] array, short[] target) 返回指定目標(biāo)的第一個匹配的起始位置數(shù)組或-1,如果不存在這樣的發(fā)生。 |
12 |
static String join(String separator, short... array) 返回包含由分離器分離所提供的短值的字符串。 |
13 |
static int lastIndexOf(short[] array, short target) 返回目標(biāo)在數(shù)組中最后一個出現(xiàn)的索引的值。 |
14 |
static Comparator<short[]> lexicographicalComparator() 返回一個比較,比較兩個 short 陣列字典順序。 |
15 |
static short max(short... array) 返回出現(xiàn)在數(shù)組中的最大值。 |
16 |
static short min(short... array) 返回出現(xiàn)在數(shù)組的最小值。 |
17 |
static short saturatedCast(long value) 返回short最接近int的值。 |
18 |
static Converter<String,Short> stringConverter() 返回使用字符串和shorts之間的一個轉(zhuǎn)換器序列化對象 Short.decode(java.lang.String) and Short.toString(). |
19 |
static short[] toArray(Collection<? extends Number> collection) 返回包含集合的每個值的數(shù)組,轉(zhuǎn)換為 short 值的方式Number.shortValue(). |
20 |
static byte[] toByteArray(short value) 返回在2元素的字節(jié)數(shù)組值大尾數(shù)法表示;相當(dāng)于 ByteBuffer.allocate(2).putShort(value).array(). |
這個類繼承了以下類方法:
java.lang.Object
使用所選擇的編輯器創(chuàng)建下面的java程序 C:/> Guava
GuavaTester.javaimport 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: " + 上一篇:Guava環(huán)境設(shè)置下一篇:Guava IntMath類