Open In App

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:



Below is the implementation to delete the last element using the two approaches:


Article Tags :