Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java ArrayList Remove Element

This small Java Example shows how to remove element from ArrayList object.
  1. //Java ArrayList Remove
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class JavaArrayListRemove {
  6.     public static void main(String args[]){
  7.        
  8.         //create new ArrayList
  9.         ArrayList<String> aListColors = new ArrayList<String>();
  10.         aListColors.add("Red");
  11.         aListColors.add("Green");
  12.         aListColors.add("Blue");
  13.        
  14.         //Java ArrayList Remove
  15.        
  16.         /*
  17.          * To remove single element from ArrayList, use
  18.          * Object remove(int index) method.
  19.          *
  20.          * This method returns object that has been removed from
  21.          * ArrayList object.
  22.          */
  23.        
  24.         //this will remove green from ArrayList
  25.         aListColors.remove(1);
  26.        
  27.         /*
  28.          * OR
  29.          * remove the element from ArrayList by passing the element itself
  30.          * instead of index using
  31.          * Object remove(Object object) method.
  32.          */
  33.        
  34.         //this will remove Blue from ArrayList
  35.         aListColors.remove("Blue");
  36.        
  37.         System.out.println(aListColors);
  38.     }
  39. }
No votes yet
Share this

Post new comment

9 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.