BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java ArrayList remove(Object obj) Method example

By Chaitanya Singh | Filed Under: java

The method remove(Object obj) removes the specified object from the list. It belongs to the java.util.ArrayList class.

public boolean remove(Object obj)

Note:

  • It returns false if the specified element doesn’t exist in the list.
  • If there are duplicate elements present in the list it removes the first occurrence of the specified element from the list.

Example

In this example we have an ArrayList<String> and we are removing few strings from the list.

package beginnersbook.com;
import java.util.ArrayList;
public class RemoveExample {
   public static void main(String args[]) {
       //String ArrayList
       ArrayList<String> al = new ArrayList<String>();
       al.add("AA");
       al.add("BB");
       al.add("CC");
       al.add("DD");
       al.add("EE");
       al.add("FF");
       System.out.println("ArrayList before remove:");
       for(String var: al){
           System.out.println(var);
       }
       //Removing element AA from the arraylist
       al.remove("AA");
       //Removing element FF from the arraylist
       al.remove("FF");
       //Removing element CC from the arraylist
       al.remove("CC");
       /*This element is not present in the list so
        * it should return false
        */
       boolean b=al.remove("GG");
       System.out.println("Element GG removed: "+b);
       System.out.println("ArrayList After remove:");
       for(String var2: al){
           System.out.println(var2);
       } 
   }
}

Output:

ArrayList before remove:
AA
BB
CC
DD
EE
FF
Element GG removed: false
ArrayList After remove:
BB
DD
EE

Comments

  1. Troy Taylor says

    October 17, 2015 at 1:54 AM

    Very clear, very helpful. Thanks ++

    Reply
  2. Deepika D says

    October 21, 2018 at 5:41 PM

    I have a question : if ArrayList list…. is defined and list.remove(Integer) is called.
    will it take that Integer parameter as index or as a element of the list ????

    Reply
    • Chaitanya Singh says

      October 29, 2018 at 1:57 PM

      Lets say we have an arraylist of type integer then using list.remove(1) will remove the element at the position 1. However if you want to remove the element that has value 1 then do it like this: list.remove(Integer.valueOf(1)).

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap