Open In App

Java Guava | Bytes.lastIndexOf() method with Examples

Last Updated : 31 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The lastIndexOf() method of Bytes Class in Guava library is used to find the last index of the given byte value in a byte array. This byte value to be searched and the byte array in which it is to be searched, both are passed as a parameter to this method. It returns an integer value which is the last index of the specified byte value. If the value is not found, it returns -1.

Syntax:

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

Parameters: This method accepts two mandatory parameters:

  • array: which is the array of byte values in which the byte value is to searched.
  • target: which is byte value to be searched for last index in the byte array.

Return Value: This method returns an integer value which is the last index of the specified byte value. If the value is not found, it returns -1.

Below programs illustrates this method:

Example 1:




// Java code to show implementation of
// Guava's Bytes.lastIndexOf() method
  
import com.google.common.primitives.Bytes;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a byte array
        byte[] arr = { 1, 2, 3, 4, 3, 5, 3, 4 };
  
        byte target = 3;
  
        // Using Bytes.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
            = Bytes.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");
        }
    }
}


Output:

Target is present at index 6

Example 2:




// Java code to show implementation of
// Guava's Bytes.lastIndexOf() method
  
import com.google.common.primitives.Bytes;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a byte array
        byte[] arr = { 3, 5, 7, 11, 13 };
  
        byte target = 17;
  
        // Using Bytes.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
            = Bytes.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");
        }
    }
}


Output:

Target is not present in the array

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads