- Guava 教程
- Guava - 首頁
- Guava - 概述
- Guava - 環境配置
- Guava - Optional 類
- Guava - Preconditions 類
- Guava - Ordering 類
- Guava - Objects 類
- Guava - Range 類
- Guava - Throwables 類
- Guava - 集合工具類
- Guava - 快取工具類
- Guava - 字串工具類
- Guava - 原生型別工具類
- Guava - 數學工具類
- Guava 有用資源
- Guava - 快速指南
- Guava - 有用資源
- Guava - 討論
Guava - Doubles 類
Doubles 是一個用於基本型別 double 的工具類。
類宣告
以下是com.google.common.primitives.Doubles類的宣告:
@GwtCompatible(emulated = true)
public final class Doubles
extends Object
欄位
| 序號 | 欄位及描述 |
|---|---|
| 1 |
static int BYTES 表示一個基本 double 值所需的位元組數。 |
方法
| 序號 | 方法及描述 |
|---|---|
| 1 |
static List<Double> asList(double... backingArray) 返回一個由指定陣列支援的固定大小列表,類似於 Arrays.asList(Object[])。 |
| 2 |
static int compare(double a, double b) 比較兩個指定的 double 值。 |
| 3 |
static double[] concat(double[]... arrays) 返回來自每個提供的陣列組合到單個數組中的值。 |
| 4 |
static boolean contains(double[] array, double target) 如果目標作為元素存在於陣列中的任何位置,則返回 true。 |
| 5 |
static double[] ensureCapacity(double[] array, int minLength, int padding) 返回一個包含與陣列相同值的陣列,但保證具有指定的最小長度。 |
| 6 |
static int hashCode(double value) 返回 value 的雜湊碼;等於呼叫 ((Double) value).hashCode() 的結果。 |
| 7 |
static int indexOf(double[] array, double target) 返回 target 值在陣列中第一次出現的索引。 |
| 8 |
static int indexOf(double[] array, double[] target) 返回陣列中指定目標的第一次出現的起始位置,如果不存在則返回 -1。 |
| 9 |
static boolean isFinite(double value) 如果 value 表示實數,則返回 true。 |
| 10 |
static String join(String separator, double... array) 返回一個包含提供的 double 值的字串,這些值根據 Double.toString(double) 轉換為字串,並由 separator 分隔。 |
| 11 |
static int lastIndexOf(double[] array, double target) 返回 target 值在陣列中最後一次出現的索引。 |
| 12 |
static Comparator<double[]> lexicographicalComparator() 返回一個按字典順序比較兩個 double 陣列的比較器。 |
| 13 |
static double max(double... array) 返回陣列中存在的最大值,使用與 Math.max(double, double) 相同的比較規則。 |
| 14 |
static double min(double... array) 返回陣列中存在的最小值,使用與 Math.min(double, double) 相同的比較規則。 |
| 15 |
static Converter<String,Double> stringConverter() 返回一個可序列化的轉換器物件,它使用 Double.valueOf(java.lang.String) 和 Double.toString() 在字串和雙精度浮點數之間進行轉換。 |
| 16 |
static double[] toArray(Collection<? extends Number> collection) 返回一個包含 collection 的每個值的陣列,這些值以 Number.doubleValue() 的方式轉換為 double 值。 |
| 17 |
static Double tryParse(String string) 將指定的字串解析為雙精度浮點值。 |
繼承的方法
此類繼承自以下類的方法:
- 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