Guayaba de Java | Método Bytes.indexOf(byte[] array, byte target) con ejemplos

El método Bytes.indexOf(byte[] array, byte target) de la clase Bytes de Guava acepta dos parámetros array y target . Si el destino existe dentro de la array, el método devuelve la posición de su primera aparición . Si el destino no existe dentro de la array, el método devuelve -1 .

Sintaxis:

public static int indexOf(array de bytes[], destino de bytes)

Parámetros: El método acepta dos parámetros:

  • array: que es la array de enteros en la que se verificará el índice de la array de destino.
  • destino: que es el valor que se buscará como un elemento en la array especificada.

Valor devuelto: el método devuelve un valor entero de la siguiente manera:

  • Devuelve la posición de la primera aparición del destino si el destino existe en la array.
  • De lo contrario, devuelve -1 si el objetivo no existe en la array.

Excepciones: el método no arroja ninguna excepción.

Los siguientes ejemplos ilustran la implementación del método anterior:

Ejemplo 1:

// Java code to show implementation of
// Guava's Bytes.indexOf(byte[] array,
// byte target) method
  
import com.google.common.primitives.Bytes;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating an byte array
        byte[] arr = { 1, 2, 3, 4, 3, 5 };
  
        byte target = 3;
  
        System.out.println("Array: "
                           + Arrays.toString(arr));
  
        System.out.println("Target: " + target);
  
        // Using Bytes.indexOf(byte[] array, byte target)
        // method to get the position of the first
        // occurrence of the specified target within array,
        // or -1 if there is no such occurrence.
        int index = Bytes.indexOf(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:

Array: [1, 2, 3, 4, 3, 5]
Target: 3
Target is present at index 2

Ejemplo 2:

// Java code to show implementation of
// Guava's Bytes.indexOf(byte[] array,
// byte target) method
  
import com.google.common.primitives.Bytes;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating an byte array
        byte[] arr = { 3, 5, 7, 11, 13 };
  
        byte target = 23;
  
        System.out.println("Array: "
                           + Arrays.toString(arr));
  
        System.out.println("Target: " + target);
  
        // Using Bytes.indexOf(byte[] array, byte target)
        // method to get the start position of the first
        // occurrence of the specified target within array,
        // or -1 if there is no such occurrence.
        int index = Bytes.indexOf(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:

Array: [3, 5, 7, 11, 13]
Target: 23
Target is not present in the array

Referencia: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/primitives/Bytes.html#indexOf(byte[], %20byte)

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 *