What does the method remove(obj o) do in java?


The remove(Object) method of the class java.util.ArrayList removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged.

Example

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      ArrayList<String> arrlist = new ArrayList<String>(5);
      arrlist.add("G");
      arrlist.add("E");
      arrlist.add("F");
      arrlist.add("M");
      arrlist.add("E");
      System.out.println("Size of list: " + arrlist.size());
      for (String value : arrlist) {
         System.out.println("Value = " + value);
      }
      arrlist.remove("E");
      System.out.println("Now, Size of list: " + arrlist.size());
      for (String value : arrlist) {
         System.out.println("Value = " + value);
      }
   }
}

Output

Size of list: 5
Value = G
Value = E
Value = F
Value = M
Value = E
Now, Size of list: 4
Value = G
Value = F
Value = M
Value = E

Updated on: 20-Feb-2020

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements