17. boolean remove(Object o)

17. boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.spy.arraylist.method;
 
import java.util.ArrayList;
 
public class Remove2 
{
 
	public static void main(String[] args) 
	{
 
 
        ArrayList<String> emp_list=new ArrayList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Vaibhav Prajapati");
 
 
		System.out.println("Retrieving Element");
 
		for (String s : emp_list) 
		{
		  System.out.println(s);
		}
 
		// This method removes the first occurrence of the specified element from this list, if it is present.
		   emp_list.remove("Bhavesh Lakhani");
 
		System.out.println("After Removing first occurence of Bhavesh Lakhani");
 
		for (String s : emp_list) 
		{
			System.out.println(s);
		}	
 
	}
}

Output:

1
2
3
4
5
6
7
8
9
10
11
Retrieving Element
Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Bhavesh Lakhani
Vaibhav Prajapati
After Removing first occurence of Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Bhavesh Lakhani
Vaibhav Prajapati

Leave a Reply

Your email address will not be published. Required fields are marked *