![]() |
VOOZH | about |
The indexOf() method in Java is used to find the index of the first occurrence of a specified element in an ArrayList.
Example 1: Here, we will use the indexOf() method to find the index of a specific element in an ArrayList.
1 2 3 The element 3 is at index: 2
ArrayList.indexOf() Methodpublic int indexOf(Object o)
Example 2: Here, we use the indexOf() method to find the first occurrence and lastIndexOf()to find the last occurrence of a specific element.
The first occurrence of 4 is: 3 The last occurrence of 4 is: 3
Example 3:If the specified element is not found in the ArrayList, the indexOf() method will return -1.
The index of 50 is: -1