Java List indexOf() MethodThe indexOf() method of List interface returns the index of the first occurrence of the specified element in this list. It returns -1 if the specified element is not present in this list. SyntaxParametersThe parameter 'o' represents the element to be searched. Throws:ClassCastException- If the type of the specified element is not compatible with this list. NullPointerException- If the specified element is null and this list does not allow null elements. ReturnThe indexOf() method returns the index of the first occurrence of the specified element if it is present in this list, else it returns -1. Example 1Test it NowOutput: Element stored at index 0 : 0 Element stored at index 1 : 1 Element stored at index 2 : 2 Element stored at index 3 : 3 Element stored at index 4 : 4 Element stored at index 5 : 5 Example 2Test it NowOutput: Element stored at Index 90 : -1 Example 3Test it NowOutput: Element stored at null : -1 Next TopicJava List |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India