Open In App

Java Guava | Bytes.indexOf(byte[] array, byte[] target) method with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

Bytes.indexOf(byte[] array, byte[] target) method of Guava’s Bytes Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1.

Syntax:

public static int indexOf(byte[] array, byte[] target)

Parameters: The method accepts two parameters:

  • array: which is the integer array in which the target array is to checked for index.
  • target: which is the array to be searched for as a sub-sequence of specified array.

Return Value: The method returns an integer value as follows:

  • It returns the start position of first occurrence of the target if the target array exists in the array.
  • Else it returns -1 if the target does not exist in the array.

Exceptions: The method does not throw any exception.

Below examples illustrate the implementation of above method:

Example 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, 4, 3 };
  
        System.out.println("Array: "
                           + Arrays.toString(arr));
  
        System.out.println("Target Array: "
                           + Arrays.toString(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");
        }
    }
}


Output:

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

Example 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, 37 };
  
        System.out.println("Array: "
                           + Arrays.toString(arr));
  
        System.out.println("Target Array: "
                           + Arrays.toString(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");
        }
    }
}


Output:

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

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



Last Updated : 06 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads