To remove a specific element, use the remove method. - Java Collection Framework

Java examples for Collection Framework:ArrayList

Introduction

It lets you remove an element based on the index number, like this: emps.remove(0);

Demo Code

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<Employee> emps = new ArrayList<Employee>();

    // add employees to array list
    emps.add(new Employee("A"));
    emps.add(new Employee("T"));
    emps.add(new Employee("K"));

    // print array list
    System.out.println(emps);//from w w  w .  j a  v a 2s. c  o m

    emps.remove(0);

    // print the array list again
    System.out.println(emps);

  }

}
class Employee{
  private String name;

  public Employee(String name) {
    this.name = name;
  }


  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }


  @Override
  public String toString() {
    return "Employee [name=" + name + "]";
  }

}

Related Tutorials