Java番石榴 |带有示例的 Doubles.lastIndexOf() 方法
Guava 库中Doubles 类的lastIndexOf()方法用于查找给定 double 值在 double 数组中的最后一个索引。这个要搜索的双精度值和要在其中搜索的双精度数组都作为参数传递给此方法。它返回一个整数值,它是指定双精度值的最后一个索引。如果未找到该值,则返回 -1。
句法:
public static int lastIndexOf(double[] array,
double target)
参数:此方法接受两个强制参数:
- 数组:这是要在其中搜索双精度值的双精度值数组。
- 目标:这是要在双精度数组中搜索最后一个索引的双精度值。
返回值:此方法返回一个整数值,它是指定双精度值的最后一个索引。如果未找到该值,则返回 -1。
下面的程序说明了这种方法:
示例 1:
// Java code to show implementation of
// Guava's Doubles.lastIndexOf() method
import com.google.common.primitives.Doubles;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a double array
double[] arr = { 1.2, 2.2, 3.2, 4.2,
3.2, 5.6, 3.2, 4.4 };
double target = 3.2;
// Using Doubles.lastIndexOf() method
// to get the index of last appearance
// of a given element in array
// and return -1 if element is
// not found in the array
int index
= Doubles.lastIndexOf(arr, target);
if (index != -1) {
System.out.println("Target is present"
+ " at index "
+ index);
}
else {
System.out.println("Target is not present"
+ " in the array");
}
}
}
输出:
Target is present at index 6
示例 2:
// Java code to show implementation of
// Guava's Doubles.lastIndexOf() method
import com.google.common.primitives.Doubles;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating a double array
double[] arr = { 1.2, 2.2, 3.2, 4.2,
3.2, 5.6, 3.2, 4.4 };
double target = 10.2;
// Using Doubles.lastIndexOf() method
// to get the index of last appearance
// of a given element in array
// and return -1 if element is
// not found in the array
int index
= Doubles.lastIndexOf(arr, target);
if (index != -1) {
System.out.println("Target is present"
+ " at index "
+ index);
}
else {
System.out.println("Target is not present"
+ " in the array");
}
}
}
输出:
Target is not present in the array
参考: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/primitives/Doubles.html#lastIndexOf(double[], %20double)