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] After removing the last element 3, the ArrayList is: [1, 1, 2, 2]
We can use the remove() method of ArrayList container in Java to remove the last element.
ArrayList provides two overloaded remove() method:
- remove(int index) : Accept index of the object to be removed. We can pass the last elements index to the remove() method to delete the last element.
- remove(Object obj) : Accept object to be removed. If the ArrayList does not contain duplicates, we can simply pass the last element value to be deleted to the remove() method, and it will delete that value.
Note: Incase the ArrayList contains duplicates, it will delete the first occurrence of the object passed as a parameter to the remove() method.
Below is the implementation to delete the last element using the two approaches:
- Program 1: Using remove(int index). Calculate the last element’s index using the size() method as:
index = ArrayList.size() - 1;
// Java program to delete last element of ArrayListimportjava.util.List;importjava.util.ArrayList;publicclassGFG {publicstaticvoidmain(String[] args){List<Integer> al =newArrayList<>();al.add(10);al.add(20);al.add(30);al.add(1);al.add(2);// Calculate index of last elementintindex = al.size() -1;// Delete last element by passing indexal.remove(index);System.out.println("Modified ArrayList : "+ al);}}chevron_rightfilter_noneOutput:Modified ArrayList : [10, 20, 30, 1]
- Program 2: Using remove(Object obj).
// Java program to delete last element of ArrayListimportjava.util.List;importjava.util.ArrayList;publicclassGFG {publicstaticvoidmain(String[] args){List<Integer> al =newArrayList<>();al.add(10);al.add(20);al.add(30);al.add(1);al.add(2);// Since all elements are unique, pass the last// elements value to delete it// Note: values are integer objectal.remove(newInteger(2));System.out.println("Modified ArrayList : "+ al);}}chevron_rightfilter_noneOutput:Modified ArrayList : [10, 20, 30, 1]
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Remove first element from ArrayList in Java
- How to remove an element from ArrayList in Java?
- Find first and last element of ArrayList in java
- ArrayList of ArrayList in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.Arraylist.indexOf() in Java
- ArrayList in Java
- Arraylist.contains() in Java
- Reverse an ArrayList in Java
- How to get ArrayList from Stream in Java 8
- Initialize an ArrayList in Java
- Custom ArrayList in Java
- Synchronization of ArrayList in Java
- Get first and last elements from ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

