java面试题网

普通会员

483

帖子

9

回复

187

积分

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

Guava Floats类

Guava Floats类

Floats是float基本类型的实用工具类。

类声明

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

@GwtCompatible(emulated=true)
   public final class Floats
      extends Object

字体

Guava Floats类_www.wityx.com

方法

Guava Floats类_www.wityx.com

继承的方法

这个类继承了以下类方法:

java.lang.Object

Floats 示例

使用所选择的任何编辑器创建下面的java程序 C:/> Guava GuavaTester.java

import java.util.List;

import com.google.common.primitives.Floats;

public class GuavaTester {

   public static void main(String args[]){
      GuavaTester tester = new GuavaTester();
      tester.testFloats();
   }

   private void testFloats(){
      float[] floatArray = {1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f};

      //convert array of primitives to array of objects
      List<Float> objectArray = Floats.asList(floatArray);
      System.out.println(objectArray.toString());

      //convert array of objects to array of primitives
      floatArray = Floats.toArray(objectArray);
      System.out.print("[ ");
      for(int i = 0; i< floatArray.length ; i++){
         System.out.print(floatArray[i] + " ");
      }
      System.out.println("]");
      //check if element is present in the list of primitives or not
      System.out.println("5.0 is in list? "+ Floats.contains(floatArray, 5.0f));

      //return the index of element
      System.out.println("5.0 position in list "+ Floats.indexOf(floatArray, 5.0f));

      //Returns the minimum		
      System.out.println("Min: " + Floats.min(floatArray));

      //Returns the maximum		
      System.out.println("Max: " + Floats.max(floatArray));	
   }
}

验证结果

使用javac编译器编译如下类

C:Guava>javac GuavaTester.java

现在运行GuavaTester看到的结果

C:Guava>java GuavaTester

看到结果

[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
[ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ]
5.0 is in list? true
5.0 position in list 4
Min: 1.0
Max: 9.0


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

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

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