Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Replace a Element in Java ArrayList?
Next article icon

Removing Element from the Specified Index in Java ArrayList

Last Updated : 31 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The remove(int index) method present in java.util.ArrayList class removes the element at the specified position in this list and shifts any subsequent elements to the left (i.e. subtracts one from their indices).

Syntax : 

public removed_element remove(int index)

Parameters: The index of the element to be removed.

Return Type: This method returns the element that was removed from the list.

Exception: This method throws IndexOutOfBoundsException if the index is out of range.

Java




// Java program to remove an element
// from an specified index from
// an ArrayList.
 
import java.util.ArrayList;
 
public class GFG {
    public static void main(String[] arg)
    {
 
        // creating an empty ArrayList with an initial
        // capacity of 5
        ArrayList<String> flower = new ArrayList<String>(5);
 
        // using add() method to add elements in the
        // ArrayList flower
        flower.add("red-rose");
        flower.add("tulip");
        flower.add("sun-flower");
        flower.add("marie-gold");
        flower.add("orchid");
 
        // printing the size of the ArrayList flower
        System.out.println("Size of list: "
                           + flower.size());
 
        // printing the ArrayList flower
        System.out.println("Flower ArrayList = " + flower);
 
        // Removing element at 3rd position from ArrayList
        // flower
        System.out.println(
            "Removing element at index = 2 ");
        flower.remove(2);
 
        System.out.println("After removing element");
 
        // printing the size of the ArrayList flower
        System.out.println("Size of list: "
                           + flower.size());
 
        // printing the ArrayList flower
        System.out.println("Flower ArrayList = " + flower);
    }
}
 
 
Output
Size of list: 5 Flower ArrayList = [red-rose, tulip, sun-flower, marie-gold, orchid] Removing element at index = 2  After removing element Size of list: 4 Flower ArrayList = [red-rose, tulip, marie-gold, orchid]

Time Complexity: O(n)
Auxiliary Space: O(1)

As constant extra space is used.

When we will try to remove an element at the index which is greater than or equal to the size of the array list, the editor will give us the IndexOutOfBound Exception on running.

Java




// Java program to show the exception
// of remove() method
 
import java.util.ArrayList;
 
public class ArrayListDemo {
   public static void main(String[] args) {
       
      // create an empty array list with an initial capacity
      ArrayList<Integer> arrlist = new ArrayList<Integer>(5);
 
      // use add() method to add elements in the deque
      arrlist.add(20);
      arrlist.add(15);
      arrlist.add(30);
      arrlist.add(45);
 
      System.out.println("Size of list: " + arrlist.size());
       
      // let us print all the elements available in list
      for (Integer number : arrlist) {
         System.out.println("Number = " + number);
      } 
       
      // Removes element at 5th position
      // which is not present in the list
      // and will therefore give IndexOutOfBound exception
      arrlist.remove(4);
 
      System.out.println("Now, Size of list: " + arrlist.size());
       
      // let us print all the elements available in list
      for (Integer number : arrlist) {
         System.out.println("Number = " + number);
      }
   }
}
 
 
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4

Time Complexity: O(n)
Auxiliary Space: O(1)

As constant extra space is used.



Next Article
How to Replace a Element in Java ArrayList?

G

gunjanpaul
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-ArrayList
  • Java-Collections
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • Removing last element from ArrayList in Java
    Given an ArrayList collection in Java, the task is to remove the last element from the ArrayList. Example: Input: ArrayList[] = [10, 20, 30, 1, 2] Output: [10, 20, 30, 1] After removing the last element 2, the ArrayList is: [10, 20, 30, 1] Input: ArrayList[] = [1, 1, 2, 2, 3] Output: [1, 1, 2, 2] Af
    2 min read
  • Remove first element from ArrayList in Java
    Given an ArrayList collection in Java, the task is to remove the first element from the ArrayList. Example: Input: ArrayList[] = [10, 20, 30, 40, 50] Output: [20, 30, 40, 50]Input: ArrayList[] = [1, 1, 2, 2, 3]Output: [1, 2, 2, 3] We can use the remove() method of ArrayList container in Java to remo
    2 min read
  • How to Replace a Element in Java ArrayList?
    To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the
    2 min read
  • How to Insert all the Collection Elements to the Specified Position in Java ArrayList?
    The element can be inserted at the collection elements to the specified position in ArrayList using Collection.addAll() method which is present in java.util.ArrayList class. If any element present at the index then that element and all its right side elements are shifted to the right side. this meth
    3 min read
  • How to Replace an Element at a Specific Index of the Vector in Java?
    The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here. Examples Input : Vector= ["
    2 min read
  • How to Add an Element at Particular Index in Java ArrayList?
    ArrayList.add() method is used to add an element at particular index in Java ArrayList. Syntax: public void add(int index, Object element) ; Parameters: index -position at which the element has to be inserted. The index is zero-based.element - the element to be inserted at the specified position. Ex
    2 min read
  • How to Swap Two Elements in an ArrayList in Java?
    We can swap two elements of Array List using Collections.swap() method. This method accepts three arguments. The first argument is the ArrayList and the other two arguments are the indices of the elements. This method returns nothing. Syntax: public static void swap(List list, int a, int b); Paramet
    2 min read
  • Java Program to Remove a Specific Element From a Collection
    remove() method is used to remove elements from a collection. It removes the element at the specified position in this list. Shifts any subsequent elements to the left by subtracts one from their indices. In simpler words, the remove() method is used for removing the element from a specific index fr
    3 min read
  • Java Program to Remove Duplicate Elements From the Array
    Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted. Example: [GFGTABS] Java // Java Program to Remove Dup
    6 min read
  • Replacing All Occurrences of Specified Element of Java ArrayList
    ArrayList is present in java.util package and it is the implementation class of List interface. It does allow the duplicates elements and also it maintains the insertion order of the elements. It dynamically resized its capacity. Though, it may be slower than standard arrays but can be helpful in pr
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences