See the documentation for ArrayList#remove(int), as in the following syntax:

list.remove(list.size() - 1)

Here is how it's implemented. elementData does a lookup on the backing array (so it can cut it loose from the array), which should be constant time (since the JVM knows the size of an object reference and the number of entries it can calculate the offset), and numMoved is 0 for this case:

public E remove(int index) {
    rangeCheck(index); // throws an exception if out of bounds

    modCount++;        // each time a structural change happens
                       // used for ConcurrentModificationExceptions

    E oldValue = elementData(index);

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // Let gc do its work

    return oldValue;
}
Answer from Nathan Hughes on Stack Overflow
🌐
Java Guides
javaguides.net › 2024 › 06 › java-arraylist-removelast-method.html
Java ArrayList removeLast() Method
June 11, 2024 - Spring Boot Microservice Tutorial Spring Boot Kafka Microservices Spring Boot + Apache Kafka Tutorial Apache Kafka Tutorials Docker Tutorials and Guides Spring Boot RabbitMQ Tutorials Interview Guides ... Angular CRUD Example with Spring Boot Spring Boot + Angular 12 CRUD Full Stack Spring Boot + Angular 8 CRUD Full Stack Spring Boot + Angular 10 CRUD Full Stack Spring Boot + React JS CRUD Full Stack React JS ( React Hooks) + Spring Boot Spring Boot Thymeleaf CRUD Full Stack Spring Boot User Registration and Login Node Js + Express + MongoDB CRUD Vue JS + Spring Boot REST API Tutorial
Discussions

MOOC Java 03_17 RemoveLast Exercise Help Needed
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
3
1
March 21, 2022
arraylist - Remove Last Element from a List in Java - Stack Overflow
In Java 21+, List is a sub-interface of SequencedCollection. This brings convenient new methods such as removeLast. More on stackoverflow.com
🌐 stackoverflow.com
java - How to remove the last string in an array element using parameters of another method named removeLast() - Stack Overflow
Here is the question of exercise and my code: Create the method public static void removeLast(ArrayList strings) in the exercise template. The method should remove the last value in the list it rec... More on stackoverflow.com
🌐 stackoverflow.com
Java* Add the removeLast method to the ArrayList class of Section 16.4. Return the removed element or null if the array list is empty. ArrayList.java /** This is a simplified implementation of an array list. */ public class ArrayList { private Object[] elements; private int currentSize; /** Constructs an empty array list. */ public
Answer to Java* Add the removeLast method to the ArrayList More on chegg.com
🌐 chegg.com
1
April 5, 2022
🌐
Reddit
reddit.com › r/learnjava › mooc java 03_17 removelast exercise help needed
r/learnjava on Reddit: MOOC Java 03_17 RemoveLast Exercise Help Needed
March 21, 2022 -

So I'm just starting the Mooc java course and i'm not sure how to get past this exercise.

Here's my code:

import java.util.ArrayList;

public class RemoveLast {

public static void main(String[] args) {

}

public static void remove (ArrayList<String> list) {

if (!list.isEmpty()) {

int last = list.size() - 1;

list.remove(last);

}

}

}

When I start using the method nothing seems to be wrong, but when I submit it, the fail message tells me "the removeLast method should not cause an exception. Make sure the method doesn't do anything to an empty list."

Thank you :)

Top answer
1 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 2
1
I believe the method is correct. I guess you should change the name of static method to removeLast or something for letting the text part know it is the method needs to call. If it didn’t work, then I don’t know why.
🌐
Baeldung
baeldung.com › home › java › java collections › guide to the java arraylist
Guide to the Java ArrayList | Baeldung
December 14, 2024 - @Test public void givenSequenc... arrayList.removeFirst()); } Similarly, to remove the last element in an existing collection, we call the instance method removeLast()....
🌐
GitHub
github.com › jumpalottahigh › JavaChallenge-MOOC-course › blob › master › week3-062.RemoveLast › src › RemoveLast.java
JavaChallenge-MOOC-course/week3-062.RemoveLast/src/RemoveLast.java at master · jumpalottahigh/JavaChallenge-MOOC-course
ArrayList<String> persons = new ArrayList<String>(); persons.add("Pekka"); persons.add("James"); persons.add("Liz"); persons.add("Brian"); · System.out.println("Persons:"); System.out.println(persons); · // sort the persons · Collections.sort(persons); · // and remove the last · removeLast(persons); ·
Author: jumpalottahigh
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 73720564 › how-to-remove-the-last-string-in-an-array-element-using-parameters-of-another-me
java - How to remove the last string in an array element using parameters of another method named removeLast() - Stack Overflow
But, you are being passed (a copy of) the reference to the ArrayList to be modified. So, when you change the contents of the list, it is changed in the calling method. ... Save this answer. ... Show activity on this post. "If the list is empty, the method does nothing" --> Check if the list is empty or not, if empty just return nothing , else delete last element(as per your requirement) ... Copypublic static void removeLast(ArrayList<String> strings) { if( (strings.isEmpty())) { return; } else { strings.remove(strings.size()-1); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › removing-last-element-from-arraylist-in-java
Removing last element from ArrayList in Java - GeeksforGeeks
July 12, 2025 - We can use the remove() method of ArrayList container in Java to remove the last element.
🌐
GeeksforGeeks
geeksforgeeks.org › java › linkedlist-removelast-method-in-java
LinkedList removeLast() Method in Java - GeeksforGeeks
July 11, 2025 - Exception: If the list is empty, calling removeLast() will throw a NoSuchElementException.
🌐
TutorialsPoint
tutorialspoint.com › java › util › arraydeque_removelast.htm
Java ArrayDeque removeLast() Method with Examples
Find the perfect tutorial for your learning journey · The Java ArrayDeque removeLast() method retrieves and removes the last element of this deque. This operation modifies the ArrayDeque instance
🌐
W3Schools
w3schools.com › java › ref_arraylist_remove.asp
Java ArrayList remove() Method
The remove() method removes an item from the list, either by position or by value. If a position is specified then this method returns the removed item.
🌐
Level Up Lunch
leveluplunch.com › java › examples › remove-last-element-arraylist
Remove last element in arraylist | Level Up Lunch
January 31, 2015 - @Test public void drop_last_element_java() { List<String> lastElementRemoved = new ArrayList<String>(); lastElementRemoved.add("one"); lastElementRemoved.add("two"); lastElementRemoved.add("three"); lastElementRemoved.remove(lastElementRemoved.size() - 1); assertEquals(2, lastElementRemoved.size()); }
🌐
Xenovation
xenovation.com › home › blog › java › how to remove the last element from list in java?
How to remove the last element from List in Java? (ArrayList, LinkedList, Vector)
June 24, 2020 - import java.util.ArrayList; import java.util.List; public class RemoveLastElementFromList { public static void main(String[] args) { List lList = new ArrayList<>(); RemoveLastElementFromList lMyClass = new RemoveLastElementFromList(); lMyClass.addTestData(lList); lMyClass.removeLastElement(lList); } public void removeLastElement(List<?> pList) { // get the index of the last entry from the list int lLastIndex = -1; if (pList.isEmpty()) { // nothing to remove } else { lLastIndex = pList.size() - 1; // if you do not need the last element anymore pList.remove(lLastIndex); } } public void addTestData(List pList) { pList.add("first"); pList.add("second"); pList.add("third"); pList.add("fourth"); } }
🌐
Reactgo
reactgo.com › home › how to remove the last element of an arraylist in java
How to remove the last element of an ArrayList in Java | Reactgo
December 24, 2022 - To remove the last element of a ArrayList, we can use the list.remove() method by passing its index list.size()-1 as an argument to it.
🌐
CodeGym
codegym.cc › java blog › java collections › how to remove an element from arraylist in java?
How to remove an element from ArrayList in Java | CodeGym
December 10, 2024 - The iterator is smarter than it may appear: remove() removes the last element returned by the iterator. As you can see, it did just what we wanted it to do :) In principle, this is everything you need to know about removing elements from an ...
🌐
W3Schools
w3schools.com › java › ref_linkedlist_removelast.asp
Java LinkedList removeLast() Method
How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum