教程 > guava 教程 > 阅读:8

guava shorts 类——迹忆客-ag捕鱼王app官网

返回 guava primitive 实用程序


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


类声明

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

@gwtcompatible
public final class shorts
   extends object

字段

序号 字段 说明
1 static int bytes 表示原始 shorts 值所需的字节数。
2 static short max_power_of_two 可以表示为 short 的两个的最大幂。

类方法

序号 方法 说明
1 static list 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) 返回其 big-endian 表示存储在字节的前 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) 返回值的哈希码; 等于调用 ((short) value).hashcode() 的结果。
10 static int indexof(short[] array, short target) 返回值 target 在数组中第一次出现的索引。
11 static int indexof(short[] array, short[] target) 返回指定 target 在数组中第一次出现的起始位置,如果不存在则返回 -1。
12 static string join(string separator, short... array) 返回一个字符串,其中包含由分隔符分隔的提供的短值。
13 static int lastindexof(short[] array, short target) 返回值 target 在数组中最后一次出现的索引。
14 static comparator lexicographicalcomparator() 返回一个比较器,该比较器按字典顺序比较两个短数组。
15 static short max(short... array) 返回数组中存在的最大值。
16 static short min(short... array) 返回数组中存在的最小值。
17 static short saturatedcast(long value) 返回最接近 value 的 short 值。
18 static converter stringconverter() 返回一个可序列化的转换器对象,该对象使用 short.decode(java.lang.string)short.tostring() 在字符串和短裤之间进行转换。
19 static short[] toarray(collection collection) 返回包含集合的每个值的数组,以 number.shortvalue() 的方式转换为短值。
20 static byte[] tobytearray(short value) 返回 2 元素字节数组中值的大端表示; 相当于 bytebuffer.allocate(2).putshort(value).array()

方法继承

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

  • java.lang.object

shorts 类示例

c:/> guava 中使用我们选择的任何编辑器创建以下 java 程序。

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};
      //将基元数组转换为对象数组
      list objectarray = shorts.aslist(shortarray);
      system.out.println(objectarray.tostring());
      //将对象数组转换为基元数组
      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;
      
      //检查元素是否存在于基元列表中
      system.out.println("5 is in list? "   shorts.contains(shortarray, data));
      //返回最小值
      system.out.println("min: "   shorts.min(shortarray));
      //返回最大值
      system.out.println("max: "   shorts.max(shortarray));
      data = 2400;
      
      //从整数中获取字节数组
      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 实用程序

查看笔记

扫码一下
查看教程更方便
网站地图