VOOZH about

URL: https://www.geeksforgeeks.org/java/copyonwritearraylist-remove-method-in-java-with-examples/

⇱ CopyOnWriteArrayList remove() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CopyOnWriteArrayList remove() method in Java with Examples

Last Updated : 6 Jan, 2020
The remove()method of CopyOnArrayList in Javais used to remove the element in the list. Syntax:
1. public E remove(int index)
2. public boolean remove(Object o)

1. remove(int index)

The remove(int index) method of CopyOnArrayList in Java is used to remove the element at the specified position in the list. Syntax:
public E remove(int index)
Parameters: This method accepts a mandatory parameter index which specifies the position of the element. Return Type: This method returns the list after deleting the specified element. Exception: This method throws ArrayIndexOutOfBounds exception if specified index is out of range i.e index is less than 0 or greater than or equal to the size of the list. Below programs illustrate the remove(int index) method of CopyOnArrayList in Java: Program 1: This program involves CopyOnArrayList remove(int index) of Integer type
Output:
CopyOnWriteArrayList: [63, 54, 81, 96]
[63, 54, 96]
Program 2: This program involves CopyOnArrayList remove(int index) of String type
Output:
CopyOnWriteArrayList: [geeks, gfg, programming]
[gfg, programming]

2. remove(Object e)

The remove(Object o) method of CopyOnArrayList in Java is used to removes the first occurrence of specified element, if it is present in the list. Syntax:
public boolean remove(Object o)
Parameters: This method accepts a mandatory parameter o, the element which is to be removed from the list, if present. Return Type: This method returns true if specified element is present in the list, else false. Below programs illustrate the remove(Object o) method of CopyOnArrayList in Java: Program 1: This program involves CopyOnArrayList remove(Object o) of Integer type
Output:
CopyOnWriteArrayList: [11, 22, 33, 22, 44]
[11, 33, 22, 44]
Program 2: This program involves CopyOnArrayList remove(Object o) of String type
Output:
CopyOnWriteArrayList: [geeks, gfg, programming]
[geeks, programming]
Comment
Article Tags:
Article Tags: