HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Collections Framework / Java ArrayList / ArrayList remove() method example

ArrayList remove() method example

ArrayList remove() removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, list remain unchanged.

1. ArrayList remove() method

The remove() method is overloaded and comes in two variants:

  • boolean remove(Object o) – removes the first occurrence of the specified element from the list. Returns true is any element was removed from the list, else false.
  • Object remove(int index) throws IndexOutOfBoundsException – removes the element at the specified position in this list. Shifts any subsequent elements to the left. Returns the removed element from the list. Throws exception if argument index is invalid.

2. ArrayList remove(Object o) example

2.1. Remove only first occurrence

Java program to remove an object from an arraylist using remove() method.

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListExample 
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
        
        System.out.println(alphabets);
        
        alphabets.remove("C");          //Element is present
        
        System.out.println(alphabets);
        
        alphabets.remove("Z");          //Element is NOT present
        
        System.out.println(alphabets);
    }
}

Program output.

[A, B, C, D]
[A, B, D]
[A, B, D]

2.2. Remove all occurrences of element

We cannot directly remove all occurrences of any element from list using remove() method. We can use removeAll() method for this purpose.

Java program to remove all the occurrences of an object from the arraylist.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class ArrayListExample 
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "A", "D", "A"));
        
        System.out.println(alphabets);
        
        alphabets.removeAll(Collections.singleton("A"));
        
        System.out.println(alphabets);
    }
}

Program output.

[A, B, A, D, A]
[B, D]

3. ArrayList remove(int index) example

Java program to remove an object by its index position from an arraylist using remove() method.

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListExample 
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
        
        System.out.println(alphabets);
        
        alphabets.remove(2);          //Index in range - removes 'C'
        
        System.out.println(alphabets);
        
        alphabets.remove(10);          //Index out of range - exception
    }
}

Program output.

[A, B, C, D]
[A, B, D]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 4
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.remove(ArrayList.java:492)
	at com.howtodoinjava.example.ArrayListExample.main(ArrayListExample.java:18)

That’s all for the ArrayList remove() method in Java.

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. maria

    April 3, 2020

    please use other classes instead of main and using remove method to remove single attribute using ArrayList

  2. Mukul Pahwa

    August 12, 2019

    ArrayList.remove(int index)
    or
    ArrayList.remove(Obj o)
    

    Returns the object removed not a boolean value

    • Lokesh Gupta

      August 12, 2019

      Reference

  3. nizar

    July 9, 2019

    alphabets.remove(“2”); //Index in range – removes ‘C’

    u can’t remove an object using string index => like (“2”)
    remove “” it should be => alphabets.remove(“2”);
    or result it will be the same [A, B, C, D]

    • nizar

      July 9, 2019

      it should be => alphabets.remove(2);

      • Lokesh Gupta

        July 9, 2019

        Thanks for pointing it out. It was a typo error and fixed now.

Comments are closed on this article!

Search Tutorials

ArrayList Methods

  • ArrayList – Introduction
  • ArrayList – add()
  • ArrayList – addAll()
  • ArrayList – clear()
  • ArrayList – clone()
  • ArrayList – contains()
  • ArrayList – ensureCapacity()
  • ArrayList – forEach()
  • ArrayList – get()
  • Arraylist – indexOf()
  • Arraylist – lastIndexOf()
  • ArrayList – listIterator()
  • ArrayList – remove()
  • ArrayList – removeAll()
  • ArrayList – removeIf()
  • ArrayList – retainAll()
  • ArrayList – sort()
  • ArrayList – spliterator()
  • ArrayList – subList()
  • ArrayList – toArray()

ArrayList Examples

  • ArrayList – Initialize arraylist
  • ArrayList – Iteration
  • ArrayList – Add/replace element
  • ArrayList – Add multiple elements
  • ArrayList – Check empty list
  • ArrayList – Remove element
  • ArrayList – Replace element
  • ArrayList – Empty arraylist
  • ArrayList – Synchronized arraylist
  • ArrayList – Compare two lists
  • ArrayList – Remove duplicates
  • ArrayList – Merge two lists
  • ArrayList – Serialization
  • ArrayList – Swap two elements
  • Convert ArrayList to Array
  • Convert Array to ArrayList
  • Convert HashSet to ArrayList
  • Convert LinkedList to ArrayList
  • Convert Vector to ArrayList
  • ArrayList vs LinkedList
  • ArrayList vs Vector

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)