java面试题网

普通会员

483

帖子

9

回复

187

积分

楼主
发表于 2019-08-26 15:24:39 | 查看: 5039| 回复: 0

Guava Shorts类

Guava Shorts类

Shorts是基本类型short的实用工具类。

类声明

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

@GwtCompatible
public final class Shorts
   extends Object

字段

Guava Shorts类_www.wityx.com

方法

Guava Shorts类_www.wityx.com

继承的方法

这个类继承了以下类方法: 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


文章来自www.wityx.com,转载请注明出处!原文地址http://www.wityx.com/post/1262_1_1.html


java面试题交流群:327440556

上一篇:Guava Ints类 下一篇:Guava Bytes类
您需要登录后才可以回帖 登录 | 立即注册

java面试题网www.wuliaokankan.cnjava建站系统提供技术支持V2.1 网站地图 © 2016-2018