📅  最后修改于: 2023-12-03 15:32:04.287000             🧑  作者: Mango
Floats.indexOf(float[] array, float target)
是 Guava 库中的一个方法,用于查找指定数组中第一个等于目标值的元素的下标。
public static int indexOf(float[] array, float target)
array
:要搜索的数组target
:要查找的值返回第一个等于目标值的元素在指定数组中的下标,如果该值不存在,则返回 -1。
import com.google.common.primitives.Floats;
public class FloatsIndexOfDemo {
public static void main(String[] args) {
float[] array = {1.2f, 3.4f, 5.6f, 7.8f, 9.1f};
float target = 5.6f;
int index = Floats.indexOf(array, target);
System.out.println("目标元素的下标是:" + index);
}
}
输出结果为:
目标元素的下标是:2
可以在调用 Floats.indexOf(float[] array, float target)
方法后,根据返回值是否为 -1 来判断指定元素是否存在于指定数组中。
Floats.indexOf(float[] array, float target)
方法可以方便地查找指定数组中第一个等于目标值的元素的下标。使用该方法可以减少程序员的开发时间,提高开发效率。