The get(index) method of CopyOnWriteArrayList returns the element at the specified index.
public E get(int index)
Parameters: The function accepts a mandatory parameter index which specifies the index of the element to be returned.
Return Value: The function returns the element at the given index.
Exceptions: The function throws IndexOutOfBoundsException if the index is out of range.
Below programs illustrate the above function:
CopyOnWriteArrayList: [32, 67, 98, 100] Element at 2nd index: 98
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at java.util.concurrent.CopyOnWriteArrayList.get(CopyOnWriteArrayList.java:388) at java.util.concurrent.CopyOnWriteArrayList.get(CopyOnWriteArrayList.java:397) at GFG.main(GFG.java:24)
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.