鍍金池/ 教程/ Java/ Guava Shorts類
Guava原語工具
Guava集合工具
Guava Chars類
Guava Shorts類
Guava CharMatcher類
Guava BigIntegerMath類
Guava Range類
Guava Bimap接口
Guava緩存工具
Guava Longs類
Guava Multiset接口
Guava Table接口
Guava Optional類
Guava LongMath類
Guava Spiltter類
Guava Preconditions類
Guava數學工具
Guava Ints類
Guava Ordering類
Guava Throwables類
Guava字符串工具
Guava Objects類
Guava Booleans類
Guava教程
Guava Bytes類
Guava CaseFormat類
Guava環(huán)境設置
Guava Doubles類
Guava Joiner類
Guava Multimap類
Guava Floats類
Guava IntMath類

Guava Shorts類

Shorts是基本類型short的實用工具類。

類聲明

以下是com.google.common.primitives.Shorts類的聲明:

@GwtCompatible
public final class Shorts
   extends Object

字段

S.N. 字段及說明
1 static int BYTES
所需要的字節(jié)數來表示一個原始short的值。
2 static short MAX_POWER_OF_TWO
兩個最大的冪可以被表示為short。

方法

S.N. 方法及說明
1 static List<Short> asList(short... backingArray)
返回由指定數組支持的固定大小的列表,類似 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,如果目標是否存在在任何地方數組元素。
6 static short[] ensureCapacity(short[] array, int minLength, int padding)
返回一個包含相同的值數組的數組,但保證是一個規(guī)定的最小長度。
7 static short fromByteArray(byte[] bytes)
返回短值,其大端表示被存儲在最前2字節(jié)的字節(jié);相當于 ByteBuffer.wrap(bytes).getShort().
8 static short fromBytes(byte b1, byte b2)
返回short值的字節(jié)表示的是給定2個字節(jié),以 big-endian 順序; 相當于 Shorts.fromByteArray(new byte[] {b1, b2}).
9 static int hashCode(short value)
返回值的哈希碼;等于調用的結果 ((Short) value).hashCode().
10 static int indexOf(short[] array, short target)
返回值目標數組的首次出現的索引。
11 static int indexOf(short[] array, short[] target)
返回指定目標的第一個匹配的起始位置數組或-1,如果不存在這樣的發(fā)生。
12 static String join(String separator, short... array)
返回包含由分離器分離所提供的短值的字符串。
13 static int lastIndexOf(short[] array, short target)
返回目標在數組中最后一個出現的索引的值。
14 static Comparator<short[]> lexicographicalComparator()
返回一個比較,比較兩個 short 陣列字典順序。
15 static short max(short... array)
返回出現在數組中的最大值。
16 static short min(short... array)
返回出現在數組的最小值。
17 static short saturatedCast(long value)
返回short最接近int的值。
18 static Converter<String,Short> stringConverter()
返回使用字符串和shorts之間的一個轉換器序列化對象 Short.decode(java.lang.String) and Short.toString().
19 static short[] toArray(Collection<? extends Number> collection)
返回包含集合的每個值的數組,轉換為 short 值的方式Number.shortValue().
20 static byte[] toByteArray(short value)
返回在2元素的字節(jié)數組值大尾數法表示;相當于 ByteBuffer.allocate(2).putShort(value).array().

繼承的方法

這個類繼承了以下類方法:

  • java.lang.Object

Shorts 示例

使用所選擇的編輯器創(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: " + 上一篇:Guava環(huán)境設置下一篇:Guava IntMath類