Javatpoint Logo
Javatpoint Logo

Java List indexOf() Method

The 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.

Syntax

Parameters

The 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.

Return

The 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 1

Test it Now

Output:

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 2

Test it Now

Output:

Element stored at Index 90 : -1

Example 3

Test it Now

Output:

Element stored at null : -1
Next TopicJava List





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA