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); } } OutputSize 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. Comment More infoAdvertise with us Next Article Removing Element from the Specified Index in Java ArrayList G gunjanpaul Follow Improve Article Tags : Java Java Programs DSA Java-Collections Java-ArrayList +1 More Practice Tags : JavaJava-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 remov 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 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); Parame 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:Java// Java Program to Remove Duplicate // Ele 6 min read Remove all Occurrences of an Element from Array in Java In Java, removing all occurences of a given element from an array can be done using different approaches that are,Naive approach using array copyJava 8 StreamsUsing ArrayList.removeAll()Using List.removeIf()Problem Stament: Given an array and a key, the task is to remove all occurrences of the speci 5 min read Replace an Element From ArrayList using Java ListIterator To replace an element from an ArrayList, the set() method of ListIterator interface can be used. set() method of ListIterator replaces the last element which is returned by the next() or previous() methods, along with the given element. Two ways of replacing the elements using ListIterator shown bel 3 min read Like