Removing Element from the Specified Index in Java ArrayList
Last Updated :
31 Mar, 2023
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.
Similar Reads
Remove all elements from the ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove all elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 3, 4] Output: ArrayList = [] Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: ArrayList = [] Using clear() method: Syntax: collection_name.clear
2 min read
Remove repeated elements from ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove repeated elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 2, 3, 4, 4, 4] Output: [1, 2, 3, 4] Input: ArrayList = [12, 23, 23, 34, 45, 45, 45, 45, 57, 67, 89] Output: [12, 23, 34, 45, 57, 67, 89] Below are
3 min read
How to remove an element from ArrayList in Java? ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. With
4 min read
How to Remove Duplicates from ArrayList in Java Given an ArrayList with duplicate values, the task is to remove the duplicate values from this ArrayList in Java. Examples: Input: List = [1, 10, 2, 2, 10, 3, 3, 3, 4, 5, 5] Output: List = [1, 10, 2, 3, 4, 5] Input: List = ["G", "e", "e", "k", "s"] Output: List = ["G", "e", "k", "s"] Using Iterator
4 min read
ArrayList removeIf() Method in Java with Examples The Java ArrayList removeIf() method is used to remove all elements from the ArrayList that satisfy a given predicate filter. The predicate is passed as a parameter to the method, and any runtime exceptions thrown during iteration or by the predicate are passed to the caller.The removeIf() method us
2 min read
Remove elements from a List that satisfy given predicate in Java Below are the methods to efficiently remove elements from a List satisfying a Predicate condition: p ==> Predicate, specifying the condition l ==> List, from which element to be removed Using iterator Below program demonstrates the removal of null elements from the list, using the Predicate Ja
5 min read