Guava - Shorts 類



Shorts 是一個用於原生型別 short 的工具類。

類宣告

以下是com.google.common.primitives.Shorts類的宣告:

@GwtCompatible
public final class Shorts
   extends Object

欄位

序號 欄位及描述
1

static int BYTES

表示一個原生 short 值所需的位元組數。

2

static short MAX_POWER_OF_TWO

可以表示為 short 的最大 2 的冪。

方法

序號 方法及描述
1

static List<Short> asList(short... backingArray)

返回一個由指定陣列支援的固定大小列表,類似於 Arrays.asList(Object[])。

2

static short checkedCast(long value)

如果可能,返回等於 value 的 short 值。

3

static int compare(short a, short b)

比較兩個指定的 short 值。

4

static short[] concat(short[]... arrays)

返回來自每個提供的陣列組合成單個數組的值。

5

static boolean contains(short[] array, short target)

如果 target 作為元素存在於陣列中的任何位置,則返回 true。

6

static short[] ensureCapacity(short[] array, int minLength, int padding)

返回一個包含與陣列相同值的陣列,但保證具有指定的最小長度。

7

static short fromByteArray(byte[] bytes)

返回其大端表示儲存在 bytes 的前 2 個位元組中的 short 值;等效於 ByteBuffer.wrap(bytes).getShort()。

8

static short fromBytes(byte b1, byte b2)

返回其位元組表示為給定的 2 個位元組的 short 值,以大端順序;等效於 Shorts.fromByteArray(new byte[] {b1, b2})。

9

static int hashCode(short value)

返回 value 的雜湊碼;等於呼叫 ((Short) value).hashCode() 的結果。

10

static int indexOf(short[] array, short target)

返回 target 在陣列中第一次出現時的索引。

11

static int indexOf(short[] array, short[] target)

返回陣列中指定目標的第一次出現的起始位置,如果不存在則返回 -1。

12

static String join(String separator, short... array)

返回一個包含由分隔符分隔的提供的 short 值的字串。

13

static int lastIndexOf(short[] array, short target)

返回 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)

返回最接近 value 的 short 值。

18

static Converter<String,Short> stringConverter()

返回一個可序列化的轉換器物件,它使用 Short.decode(java.lang.String) 和 Short.toString() 在字串和 shorts 之間進行轉換。

19

static short[] toArray(Collection<? extends Number> collection)

返回一個包含集合中每個值的陣列,以 Number.shortValue() 的方式轉換為 short 值。

20

static byte[] toByteArray(short value)

返回 value 在 2 元素位元組陣列中的大端表示;等效於 ByteBuffer.allocate(2).putShort(value).array()。

繼承的方法

此類繼承自以下類的方法:

  • java.lang.Object

Shorts 類的示例

使用您選擇的任何編輯器建立以下 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] + " ");
      }
   }
}

驗證結果

使用javac編譯器編譯該類,如下所示:

C:\Guava>javac GuavaTester.java

現在執行 GuavaTester 以檢視結果。

C:\Guava>java GuavaTester

檢視結果。

[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 
guava_primitive_utilities.htm
廣告
© . All rights reserved.