The indexOf() method of java.util.AbstractSequentialList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
public int indexOf(Object o)
Parameters: This method takes Object o as parameter which is the element to search for.
Return Value: This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Exception: This method throws:
- ClassCastException: if the type of the specified element is incompatible with this list.
- NullPointerException: if the specified element is null and this list does not permit null elements.
Below are the examples to illustrate the indexOf() method.
AbstractSequentialList: [10, 20, 30, 40, 50] Index of 30: 2
LinkedListlist: [10, 20, 30, 40, 50] Index of 100: -1
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.