- 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