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
🌐
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.
Discussions

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* 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
provide a removelast method for the arraylist implementation above that removes the last element. if the current size is more than 25 percent full, the method sunoly removes the last
Answer to provide a removelast method for the arraylist More on chegg.com
🌐 chegg.com
1
October 31, 2023
Java - remove last known item from ArrayList - Stack Overflow
OK, so here is my ArrayList: private List clients = new ArrayList (); and here is what I am trying to do: I am trying to remove the last known item from the More on stackoverflow.com
🌐 stackoverflow.com
November 8, 2011
🌐
Java Guides
javaguides.net › 2024 › 06 › java-arraylist-removelast-method.html
Java ArrayList removeLast() Method
June 11, 2024 - The removeLast method can be used to remove the last element of the ArrayList.
🌐
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.
🌐
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 ...
Find elsewhere
🌐
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.
🌐
Chegg
chegg.com › engineering › computer science › computer science questions and answers › provide a removelast method for the arraylist implementation above that removes the last element. if the current size is more than 25 percent full, the method sunoly removes the last
Solved provide a removelast method for the arraylist | Chegg.com
October 31, 2023 - public void removeLast() { // Step 1: Check if the current size is greater than 0 if (currentSize > 0) { } ] ... Refer to the ArrayList implementation below: public class ArrayList \& private Object[] elements; private int currentSize; public ArrayList() \{ final int INITIAL_SIZE = 10; elements = new Object[INITIAL_SIZE]; currentSize =0; \} public int size() \{return currentSize; \} \}
🌐
Reddit
reddit.com › r/powershell › is there an easy and elegant way of removing the last element of an array?
r/PowerShell on Reddit: Is there an easy and elegant way of removing the last element of an array?
January 14, 2025 -

Edit: Solved

It's much more flexible to use generic lists instead of arrays. Arrays are immutable and should not be used when there is a need to add or remove elements. Another option is to use array lists, but others reported that they are deprecated and that generic lists should be used instead.

Thank you all for the help!

-------------

PowerShell 7, an array like $array = @()

Like the title say - is there?

The solutions I've found online are all wrong.

- Array slicing

$array = $array[0..($array.Length - 2)]

This does not work if the array length is 1, because it resolves to $array[0..-1]. Step-by-step debugging shows that instead of deleting the last remaining element of the array, it will duplicate that element. The result will be an array of 2 elements, not 0.

- Select-Object

$array = $array | Select-Object -SkipLast 1

This does not work well with Hashtables as array elements. If your array elements are Hashtables, it will convert them to System.Collections.Hashtable. Hashtable ($example = @{}) and System.Collection.Hashtable are not the same type and operations on those two types are different (with different results).

Edit for the above: There was a typo in that part of my code and it returned some nonsense results. My bad.

- System.Collections.ArrayList

Yes, you can convert an array to System.Collection.ArrayList, but you are then working with System.Collections.ArrayList, not with an array ($array = @()).

----------------

One solution to all of this is to ask if the array length is greater than one, and handle arrays of 1 and 0 elements separately. It's using an if statement to simply remove the last element of an array, which is really bad.

Another solution is to loop through an array manually and create a new one while excluding the last element.

And the last solution that I've found is not to use arrays at all and use generic lists or array lists instead.

Is one of these options really the only solution or is there something that I'm missing?

🌐
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); } }
🌐
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
import java.util.ArrayList; import java.util.Collections; · public class RemoveLast { public static void removeLast(ArrayList<String> list) { int i = list.size(); list.remove(i-1); } · public static void main(String[] args) { // Here an example what you can do with the method ·
Author: jumpalottahigh
🌐
Microsoft Learn
learn.microsoft.com › ja-jp › dotnet › api › java.util.arraylist.removelast
ArrayList.RemoveLast Method (Java.Util) | Microsoft Learn
[<Android.Runtime.Register("removeLast", "()Ljava/lang/Object;", "GetRemoveLastHandler", ApiSince=35)>] abstract member RemoveLast : unit -> Java.Lang.Object override this.RemoveLast : unit -> Java.Lang.Object ... Added in 21. Java documentation for java.util.ArrayList.removeLast().
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.collections › remove-last.html
removeLast | Core API – Kotlin Programming Language
Removes the last element from this mutable list and returns that removed element, or throws NoSuchElementException if this list is empty · Thanks for your feedback