Java ArrayList remove elements

Introduction

To remove elements from ArrayList in Java

arrayList.remove(object); //remove by object 
arrayList.remove(index); //remove by index

Full source

import java.util.ArrayList; 
 
public class Main { 
  public static void main(String args[]) { 
    // Create an array list. 
    ArrayList<String> al = new ArrayList<String>(); 
     /*  ww  w.jav  a  2  s .com*/
    // Add elements to the array list. 
    al.add("SQL"); 
    al.add("Java"); 
    al.add("Javascript"); 
    al.add("CSS"); 
    al.add("HTML"); 
    al.add("Demo2s.com"); 
    al.add(1, "Hi"); 
 
    System.out.println("Size of al after additions: " + 
                       al.size()); 
 
    // Remove elements from the array list. 
    al.remove("CSS"); 
    al.remove(2); 
 
    System.out.println("Size of al after deletions: " + 
                       al.size()); 
  } 
}



PreviousNext

Related