![]() |
VOOZH | about |
The indexOf(Object o) method of CopyOnWriteArrayList returns the first occurrence of the element passed in the list. It returns -1 if the element is not present in the list.
Syntax:
public int indexOf(Object o)
Parameters: The function accepts a parameter o whose first occurrence is to be returned.
Return Value: The function returns the first occurrence of the element. It returns -1 if the element is not present in the list.
Below programs illustrate the above function:
Program 1:
CopyOnWriteArrayList: [32, 67, 67, 100] indexOf value: 1
Program 2:
CopyOnWriteArrayList: [32, 67, 67, 100] indexOf value: -1
The indexOf(E e, int index) method of CopyOnWriteArrayList returns the first occurrence of the element passed in the list after position index. It returns -1 if the element is not present in the list.
Syntax:
public int indexOf(E e, int index)
Return Value: The function returns the first occurrence of the element after position index. It returns -1 if the element is not present in the list.
Exceptions: The function throws an IndexOutOfBoundsException if the specified index is negative.
Below programs illustrate the above function:
Program 1:
Output:
CopyOnWriteArrayList: [32, 67, 67, 67] indexOf value: 2
Program 2:
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.concurrent.CopyOnWriteArrayList.indexOf(CopyOnWriteArrayList.java:198) at java.util.concurrent.CopyOnWriteArrayList.indexOf(CopyOnWriteArrayList.java:263) at GFG.main(GFG.java:24)