Saturday 31 May 2014

ArrayList : remove(Object o) : Remove first occurrence of Specific Object

public boolean remove(Object o)
Removes the first occurrence of the specified element from this list if it is present. Return true if this list contained the specified element.

import java.util.*;

class ArrayListRemoveObject{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(10);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  Integer i1 = new Integer(10);
  System.out.print("\nIs 10 removed ");
  System.out.println(myList.remove(i1));
  
  i1 = new Integer(50); 
  System.out.print("Is 50 removed ");
  System.out.println(myList.remove(i1));
  
  System.out.println("\nElements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[10, 20, 30, 10]

Is 10 removed true
Is 50 removed false

Elements in myList are
[20, 30, 10]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment