Removing last element from ArrayList in Java Last Updated : 26 Jan, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // Java program to delete last element of ArrayList import java.util.List; import java.util.ArrayList; public class GFG { public static void main(String[] args) { List<Integer> al = new ArrayList<>(); al.add(10); al.add(20); al.add(30); al.add(1); al.add(2); // Calculate index of last element int index = al.size() - 1; // Delete last element by passing index al.remove(index); System.out.println("Modified ArrayList : " + al); } } Output: Modified ArrayList : [10, 20, 30, 1] Program 2: Using remove(Object obj). Java // Java program to delete last element of ArrayList import java.util.List; import java.util.ArrayList; public class GFG { public static void main(String[] args) { List<Integer> al = new ArrayList<>(); 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 object al.remove(new Integer(2)); System.out.println("Modified ArrayList : " + al); } } Output: Modified ArrayList : [10, 20, 30, 1] Comment More infoAdvertise with us Next Article Remove all Occurrences of an Element from Array in Java H harsh.agarwal0 Follow Improve Article Tags : Misc Java Java Programs Java-ArrayList Practice Tags : JavaMisc Similar Reads 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 Removing Element from the Specified Index in Java ArrayList 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 3 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 Java Program to Remove an Element from ArrayList using ListIterator ListIterator.remove() method removes the last element from the list that was returned by next() or previous() cursor positions. It can be called only once per call to next or previous. It can be made only if the operation â add(E) has not called after the last call to next or previous. Internal work 4 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 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 Like