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