Guayaba de Java | Método Longs.lastIndexOf() con ejemplos

El método lastIndexOf() de Longs Class en la biblioteca Guava se usa para encontrar el último índice del valor largo dado en una array larga. Este valor largo que se buscará y la array larga en la que se buscará, ambos se pasan como parámetro a este método. Devuelve un valor entero que es el último índice del valor largo especificado. Si no se encuentra el valor, devuelve -1.

Sintaxis:

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

Parámetros: Este método acepta dos parámetros obligatorios:

  • array: que es la array de valores largos en la que se busca el valor largo.
  • objetivo: que es un valor largo para buscar el último índice en la array larga.

Valor devuelto: este método devuelve un valor entero que es el último índice del valor largo especificado. Si no se encuentra el valor, devuelve -1.

Los siguientes programas ilustran este método:

Ejemplo 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");
        }
    }
}
Producción:

Target is present at index 6

Ejemplo 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");
        }
    }
}
Producción:

Target is not present in the array

Referencia: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/primitives/Longs.html#lastIndexOf-long:A-long-

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *