📌  相关文章
📜  Java番石榴 |带有示例的 Longs.lastIndexOf() 方法

📅  最后修改于: 2022-05-13 01:54:45.761000             🧑  作者: Mango

Java番石榴 |带有示例的 Longs.lastIndexOf() 方法

Guava 库中Longs 类lastIndexOf()方法用于查找给定 long 值在 long 数组中的最后一个索引。这个要搜索的 long 值和要在其中搜索的 long 数组都作为参数传递给此方法。它返回一个整数值,它是指定 long 值的最后一个索引。如果未找到该值,则返回 -1。

句法:

public static int lastIndexOf(long[] array,
                              long target)

参数:此方法接受两个强制参数:

  • 数组:它是长值数组,长值要在其中搜索。
  • 目标:这是要在长数组中搜索最后一个索引的长值。

返回值:此方法返回一个整数值,它是指定长值的最后一个索引。如果未找到该值,则返回 -1。

下面的程序说明了这种方法:

示例 1:

// Java code to show implementation of
// Guava's Longs.lastIndexOf() method
  
import com.google.common.primitives.Longs;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a long array
        long[] arr = { 1, 2, 3, 4, 3, 5, 3, 4 };
  
        long target = 3;
  
        // Using Longs.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
            = Longs.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 Longs.lastIndexOf() method
  
import com.google.common.primitives.Longs;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a long array
        long[] arr = { 3, 5, 7, 11, 13 };
  
        long target = 17;
  
        // Using Longs.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
            = Longs.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/21.0/api/docs/com/google/common/primitives/Longs.html#lastIndexOf-long:A-long-