Guava教程

Guava Doubles類

 

Doubles是double基本類型的實用工具類。

 

類聲明

 

以下是com.google.common.primitives.Doubles類的聲明:

 

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

 

字段

 

 

方法

 

 

方法繼承

 

這個類繼承了以下類方法:

java.lang.Object

 

Doubles 示例

 

使用所選擇的任何編輯器創建下面的java程序 C:/> Guava
GuavaTester.java

 

import java.util.List;

import com.google.common.primitives.Doubles;

public class GuavaTester {

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

   private void testDoubles(){
      double[] doubleArray = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

      //convert array of primitives to array of objects
      List<Double> objectArray = Doubles.asList(doubleArray);
      System.out.println(objectArray.toString());

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

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

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

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

 

驗證結果

 

使用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

 

 

 

 

轉載自并發編程網-ifeve.com

全部教程
广东36选7开奖公告