![]() |
VOOZH | about |
The listIterator() method of CopyOnWriteArrayList returns an listIterator over the elements in this list in proper sequence. The listIterator does NOT support the remove, set or add methods.
Syntax:
public ListIterator listIterator()
Parameters: The function does not accept any parameters.
Return Value: The function returns a list iterator over the elements in this list (in proper sequence)
Below programs illustrate the above function:
Program 1:
CopyOnWriteArrayList: [32, 67, 67, 100] The iterator values of CopyOnWriteArrayList are: 32 67 67 100
The listIterator(index) method of CopyOnWriteArrayList returns an listIterator over the elements in this list in proper sequence starting from a particular index. The listIterator does NOT support the remove, set or add methods.
Syntax:
public ListIterator listIterator(int index)
Parameters: The function accepts a parameter index which specifies the index of the first element to be returned from the list iterator.
Return Value: The function returns a list iterator over the elements in this list starting from the specified index (in proper sequence).
Exceptions: The function throws IndexOutOfBoundsException if the index is out of range.
Below programs illustrate the above function:
Program 1:
CopyOnWriteArrayList: [gopal, gfg, jgec, sudo] The iterator values of CopyOnWriteArrayList are: jgec sudo
Program 2:
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6 at java.util.concurrent.CopyOnWriteArrayList.listIterator(CopyOnWriteArrayList.java:1107) at GFG.main(GFG.java:25)