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

Is there an easy and elegant way of removing the last element of an array?
Select-Object -SkipLast should work. Can you demonstrate the issue you are seeing with hashtables? If I run @{}.GetType() the reported type is System.Collections.Hashtable. And if I run: $Array = @{}, @{}, @{} | select -SkipLast 1 $Array[0].GetType() I still get the expected: System.Collections.Hashtable type. Anyway, if you for whatever reason don't want to use Select-Object the cleanest I can think of would be this: $MyArray = @(Get-ChildItem C:\) if ($MyArray.Count -gt 0) { [array]::Resize([ref] $MyArray, $MyArray.Count - 1) } Do keep in mind though that this only works if it's an actual array, hence the need to wrap it in @() so that even if it returns 1 item I still get an array. More on reddit.com
🌐 r/PowerShell
38
20
January 14, 2025
java - Removing the last element of an ArrayList - Stack Overflow
I'm new to Java and I'm stuck with an exercise I've been trying to solve for over a week now and I don't know what I'm doing wrong. I need to delete the last elements of an ArrayList, an integer in this case. The problem is that when I run the test, it still returns the old values. Copypublic static void removeLastOccurrence... More on stackoverflow.com
🌐 stackoverflow.com
How does an ArrayList remove an element from an array?
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 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. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar 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: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) 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/javahelp
6
10
March 15, 2023
Why is it that removing an element from the start of an array O(n) while at the end O(1)?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. 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/learnprogramming
8
2
September 16, 2023
🌐
W3Schools
w3schools.com › java › ref_arraylist_remove.asp
Java ArrayList remove() Method
If a value is specified and multiple elements in the list have the same value then only the first one is deleted. If the list contains integers and you want to delete an integer based on its value you will need to pass an Integer object. See More Examples below for an example. ... T refers to the data type of items in the list. Remove an integer from the list by position and by value: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(8); list.add(9); list.add(1); list.remove(Integer.valueOf(1)); // Remove by object list.remove(1); // Remove by index System.out.println(list); } }
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › ArrayList.html
ArrayList (Java SE 21 & JDK 21)
January 20, 2026 - Removes from this list all of its elements that are contained in the specified collection. ... Removes and returns the first element of this collection (optional operation). ... Removes all of the elements of this collection that satisfy the given predicate. ... Removes and returns the last element of this collection (optional operation).
🌐
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?

🌐
Baeldung
baeldung.com › home › java › java list › removing an element from an arraylist
Removing an Element From an ArrayList | Baeldung
April 4, 2025 - As we can see, removeLast() is pretty straightforward to use and easier to understand. Therefore, if we’re using JDK 21 or higher, this method can be a good option for removing the last element from a List. Sometimes, we want to remove an element from an ArrayList while we’re looping 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 ...
Find elsewhere
🌐
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 - The fastest way is to use the remove(pList.size()-1) of the List interface to remove the last entry. import java.util.ArrayList; import java.util.List; public class RemoveLastElementFromList { public static void main(String[] args) { List lList ...
🌐
Quora
quora.com › How-do-you-remove-the-first-and-last-elements-from-a-Java-List
How to remove the first and last elements from a Java List - Quora
Answer (1 of 2): You’re not specifying the list type to be used. The most efficient way to remove the head and tail nodes of a list depends on the concrete list. The general solution (ignoring bound checks for now) is to do the following: [code]List list = ...; list.remove(0); list.remov...
🌐
Tutorjoes
tutorjoes.in › Java_example_programs › remove_the_given_element_from_an_arraylist_in_java
Write a Java program to Remove the given element from an ArrayList
import java.util.ArrayList; public class Remove_GivenElement { public static void main(String[] args) { ArrayList<String> fru_list = new ArrayList<String>(); fru_list.add("Pineapple"); fru_list.add("Papaya"); fru_list.add("Mulberry"); fru_list.add("Apple"); fru_list.add("Banana"); fru_list.add("Cherry"); fru_list.add("Guava"); fru_list.add("Watermelon"); System.out.println("Given ArrayList : "+fru_list); //Removing first occurrence of "Mulberry" fru_list.remove("Mulberry"); //Removing first occurrence of "Guava" fru_list.remove("Guava"); System.out.println("\nRemove the given Element from an ArrayList.."); System.out.println(fru_list); } }
🌐
Tutorialride
tutorialride.com › java-collection-framework-programs › add-retrieve-and-remove-element-from-arraylist.htm
Add, retrieve and remove element from ArrayList
import java.util.*; class ArrayListDemo { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); System.out.println("Size of ArrayList: "+al.size()); //Adding the elements al.add("Java"); al.add("JDBC"); System.out.println("Elements of first ArrayList: "+al); ArrayList<String> al2 = new ArrayList<String>(); al2.add("EJB"); al2.add("Struts"); //Adding the both array al2.addAll(al); System.out.println("Elements of second ArrayList: "+al2); //remove the element al2.remove("EJB"); System.out.println("Elements of ArrayList after deletion: "+al2); System.out.println("Size of ArrayList: "+al2.size()); //Retriving 2nd index element System.out.println("The element at 2nd index is: "+al2.get(2)); } } Output:
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java arraylist remove() method
Java ArrayList remove() Method | PrepInsta
December 13, 2022 - If index is passed as parameter , it removes the element from the specified position If object is passed as parameter , it removes the first occurrence of the object and returns true if object is present in the list. ... When index is passed as parameter. ... package com.company; import java.util.ArrayList; // Main class public class prepInsta { // Main driver method public static void main (String[]args) { //Creating an Integer ArrayList List < Integer > list = new ArrayList <> (); // adding elements in the list list.add (1); list.add (2); list.add (3); list.add (11); list.add (21); // calling remove() method using index list.remove (1); list.remove (1); // Printing the updated ArrayList System.out.println (list); } }
🌐
Sarthaks eConnect
sarthaks.com › 3493086 › how-to-remove-elements-from-a-java-linkedlist
How to remove elements from a Java LinkedList? - Sarthaks eConnect | Largest Online Education Community
April 25, 2023 - LIVE Course for free · You can remove elements from a Java LinkedList using several different methods. Here are some of the most commonly used methods:
🌐
Tutorjoes
tutorjoes.in › Java_example_programs › retrieve_and_remove_the_tail_last_item_in_java
Write a Java program to Retrieve and Remove the tail/last item from the LinkedList
import java.util.LinkedList; public class Retrieve_Remove { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add(18); list.add("JAVA"); list.add(54.78); list.add('J'); list.add(true); System.out.println("LinkedList Items : " + list); System.out.println("Tail Item : " + list.pollLast()); System.out.println("LinkedList Items : " + list); System.out.println("Tail Item : " + list.pollLast()); System.out.println("LinkedList Items : " + list); } }
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › collections › implementations › list.html
List Implementations
Think of ArrayList as Vector without the synchronization overhead. If you frequently add elements to the beginning of the List or iterate over the List to delete elements from its interior, you should consider using LinkedList. These operations require constant time in a LinkedList and linear time in an ArrayList.
🌐
Talkerscode
talkerscode.com › howto › how-to-remove-an-element-from-an-array-java.php
How To Remove An Element From An Array Java
As a result, we will remove all ArrayList elements whose names begin with the letter 'b' using the removeIf() method.
🌐
Kuros
kuros.in › java › collection-list-arraylist-linked-list
List, ArrayList, LinkedList - Kuros.in
January 17, 2019 - [A, B, C, D] [1, A, B, C, D, 2] The first value = 1 The last value = 2 The value removed from the first position of list = 1 The value removed from the last position of list = 2 [A, B, C, D] Similar to the ArrayList, we have one more class the Vector class, the only difference between Vector class and ArrayList is that the methods of Vector class are synchronized, and hence are thread safe, where as the methods of ArrayList is not synchronized.
🌐
Hero Vired
herovired.com › learning-hub › blogs › difference-between-arraylist-and-linkedlist
Difference Between ArrayList and LinkedList in Java : Hero Vired
September 5, 2024 - ArrayList add timing is: 15754300 ns LinkedList add timing is: 52523400 ns ArrayList access timing is: 2217300 ns LinkedList access timing is: 174634999 ns ArrayList remove timing is: 34592300 ns LinkedList remove timing is: 95646500 ns ... So in this example, we have performed a performance test for both ArrayList and LinkedList. The code includes the performance test of adding the elements to the lists, accessing the elements from the lists, and also removing an element from the lists.