This post will discuss about Guava Ints.indexOf() and Ints.lastIndexOf() methods in Java.

While working with arrays of primitive int values in Java, you might need to search for a specific element or a subarray within a larger array. For example, you might want to find the index of the first or the last occurrence of a certain value, or check if a certain sequence of values is present in the array. However, the standard Java library does not provide any built-in methods for these operations on primitive arrays. You can use the Arrays class to perform binary search, sort, copy, or compare arrays, but not to find the index of an element or a subarray.

Fortunately, the Guava library provides an alternative for this. The Ints class from Guava contains static methods for working with primitive int values and arrays. In this post, we will focus on two of these methods: indexOf() and lastIndexOf(), which can help us find the position of a specific int value or a subarray within an int array.

1. Overview of indexOf() and lastIndexOf() methods

The indexOf() and lastIndexOf() methods are similar to the ones that are available in the String class, but they operate on int arrays instead of strings. They allow us to search for the first or the last occurrence of a given int value within an int array, and return the index where they are found, or -1 if they are not found. The syntax of these methods is as follows:

 
These method accepts two parameters: an array of int values, and a target int value. These method returns an int value representing the index of the first occurrence of the target within the array, or -1 if not found. They also throw NullPointerException if array is null.

 
There is another overloaded version of the indexOf() method, which accept different parameters. The above version take a single int value as the target to search for, while this version take an int subarray as the target. Here’s the syntax:

 
This version searches the target subarray within the array, return index of the first occurrence of the target within the array, or -1 if the subarray is not found or is longer than the main array. This version throw a NullPointerException if either array or target is null.

2. Usage of indexOf() and lastIndexOf() methods

To use these methods, we need to import the Ints class from the com.google.common.primitives package. Then we can call these methods on any int array that we have. For example, here is how we can use the indexOf() method to find the index of the first occurrence of 3 in the array:

Download Code

 
As you can see, the indexOf() method returns 2, which is the index of the first occurrence of 3 in the array. If we change the target to 6, the method will return -1, indicating that 6 is not present in the array. We can also use the lastIndexOf() method to find the index of the last occurrence of 3 in this array:

Download Code

 
The overloaded version of the indexOf() method can be used to search for a subarray within an array. For example, here is how to find the index of the first occurrence of sub in arr.

Download Code

3. Benefits of indexOf() and lastIndexOf() methods

The indexOf() and lastIndexOf() methods have some advantages over using a loop or Arrays.binarySearch():

  1. They are more concise and readable. These methods help us write code that is clear and expressive, as the intended outcome is easily visible.
  2. They can also be combined with other methods from the Ints class, such as concat(), asList(), toArray(), min(), max(), join(), and so on, to perform more complex operations on int arrays.
  3. They do not require the array to be sorted.

4. Limitations of indexOf() and lastIndexOf() methods

However, there are some limitations and caveats that you should be aware of when using the Ints.indexOf() and Ints.lastIndexOf() methods:

  1. They performs a linear search, which means that it has a time complexity of O(n), where n is the length of the array. This can be inefficient if the array is large or if you need to perform multiple searches on it.
  2. They only returns the index of the first or the last occurrence of the target value. If you need to find all occurrences, you will have to use some other method.
  3. They only works for primitive int arrays. If you have an array of Integer objects or other types, you will have to use some other method.

5. Conclusion

In this post, we have focused on Int.indexOf() and Int.lastIndexOf() methods to search for the first or the last occurrence of an int value or an int subarray within an int array. We have explained how to use these methods, what parameters they accept, what values they return, and what exceptions they throw. We have also provided some examples to illustrate their usage, and discussed their advantages and limitations.

If you want to learn more about these methods, you can check out the Guava official website or its GitHub repository.