目录

Shorts

Shorts是原始类型short的实用程序类。

Class 声明 (Class Declaration)

以下是com.google.common.primitives.Shorts类的声明 -

@GwtCompatible
public final class Shorts
   extends Object

字段 (Fields)

Sr.No 领域和描述
1

static int BYTES

表示原始短值所需的字节数。

2

static short MAX_POWER_OF_TWO

最大的两个幂可以表示为短。

方法 (Methods)

Sr.No 方法和描述
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)

比较两个指定的短值。

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)

返回一个包含与array相同值的数组,但保证指定的最小长度。

7

static short fromByteArray(byte[] bytes)

返回其big-endian表示存储在前2个字节字节中的short值; 相当于ByteBuffer.wrap(bytes).getShort()。

8

static short fromBytes(byte b1, byte b2)

以big-endian顺序返回其字节表示为给定2个字节的short值; 相当于Shorts.fromByteArray(new byte [] {b1,b2})。

9

static int hashCode(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)

返回一个字符串,其中包含由separator分隔的提供的short值。

13

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

返回数组中值target的最后一次出现的索引。

14

static Comparator《short[]》 lexicographicalComparator()

返回一个比较器,按字典顺序比较两个短数组。

15

static short max(short... array)

返回数组中存在的最大值。

16

static short min(short... array)

返回数组中存在的最小值。

17

static short saturatedCast(long value)

返回值最接近的short。

18

static Converter《String,Short》 stringConverter()

返回一个可序列化的转换器对象,它使用Short.decode(java.lang.String)和Short.toString()在字符串和短路之间进行转换。

19

static short[] toArray(Collection《? extends Number》 collection)

返回一个包含collection的每个值的数组,以Number.shortValue()的方式转换为short值。

20

static byte[] toByteArray(short value)

返回2元素字节数组中的big-endian值表示形式; 相当于ByteBuffer.allocate(2).putShort(value).array()。

方法继承 (Methods Inherited)

该类继承以下类中的方法 -

  • java.lang.Object

短裤类的例子

使用您选择的任何编辑器在C:/》 Guava.创建以下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 
↑回到顶部↑
WIKI教程 @2018