Since in Java, arrays are non-resizable, you will have to copy everything into a new, shorter array.

Arrays.copyOf(original, original.length-1)
Answer from Marko Topolnik on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ delete-an-element-from-the-end-of-an-array
Delete an Element from the end of an array - GeeksforGeeks
November 8, 2024 - // Java program to delete an element from the end of an array // using in-built methods import java.util.*; class GfG { public static void main(String[] args) { ArrayList<Integer> arr = new ArrayList<> (Arrays.asList(10, 20, 30, 40)); System.out.println("Array before deletion"); for (int ele : arr) System.out.print(ele + " "); // Remove the last element from the array arr.remove(arr.size() - 1); System.out.println("\nArray after deletion"); for (int ele : arr) System.out.print(ele + " "); } } Python ยท
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 Array remove last stored element - Stack Overflow
In my program, I have 2 array (x,y) that stores value taken from the user. When a negative value is entered, the program should stop asking for new values. Until here everything works fine. However... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why is the list method which returns all but the last element named init?

Just some observations:

  • head, tail, init, last are all four-character words

  • Haskell uses these same operations

  • Erlang also seems to use these terms

  • Okasaki's book/thesis also contains these, language is Standard ML

Scala borrowing from these functional languages (particularly ML and Haskell), my guess is the terminology was inspired from these.

More on reddit.com
๐ŸŒ r/scala
7
6
December 11, 2015
[C#] what's the correct way to remove every nth term for a list?
tec5c is not wrong; list.Where((x, i) => i % nth == 0); Or you can use != to get the opposite More on reddit.com
๐ŸŒ r/learnprogramming
6
4
November 6, 2015
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ removing an element from an array in java
Removing an Element from an Array in Java | Baeldung
June 12, 2024 - We can also use the previously discussed removeAnElementWithAGivenIndex() method to remove the last element of an array.
๐ŸŒ
Medium
medium.com โ€บ @iamdarius โ€บ 4-ways-to-remove-the-last-element-from-an-array-in-javascript-17749b12be0c
Learn 4 Ways to Remove the Last Element from an Array in JavaScript | by Darius Moore | Medium
September 8, 2022 - Splice is a powerful method that can be used to add new elements to an array, remove elements from an array, or perform both actions simultaneously. Unlike slice, splice mutates the original array that it is being invoked on. Below, the start count (the first parameter) is given an argument of -1, and the delete count (the second parameter), is 1. These two arguments indicate that the delete operation will begin at the last element of the array, and will only delete one item, respectively.
๐ŸŒ
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?

๐ŸŒ
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.
๐ŸŒ
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...
Find elsewhere
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-remove-array-elements
Java Remove Array Elements: Methods and Examples | DigitalOcean
May 2, 2025 - You canโ€™t directly remove elements from arrays in Java because arrays are fixed-size, meaning their size is determined at the time of creation and cannot be changed later.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-delete-the-last-n-elements-from-an-array-in-Java
How to delete the last n elements from an array in Java - Quora
Answer: Array lengths are immutable. You can * create a new, smaller array (having a size of the original array minus n) and use System.arraycopy(โ€ฆ) to copy the elements you want to keep over from the old to the new array - this is most likely the best option, unless your arrays are huge; the ...
๐ŸŒ
Quescol
quescol.com โ€บ home โ€บ java program to delete element at end of array
Java Program to Delete Element at End of Array - Quescol
May 8, 2025 - Our program will delete the element from end (which is specified by the user) of the given array. ... Case 1: If the given array is {1, 2, 3, 4}. Then output should be {1, 2, 3}. Case 2: If the given array is {9, 2, 4, 8. Then output should be {9, 2, 4}. import java.util.*; public class Main { public static void main(String[] args) { int lastElement; Scanner sc = new Scanner(System.in); System.out.println("Java Program to delete last element from Array"); System.out.print("Enter the size of array: "); int size = sc.nextInt(); int arr[] = new int[size]; for(int i=0; i<size; i++) { System.out.print("Please give value for index "+ i +" : "); arr[i] = sc.nextInt(); } lastElement=arr[size-1]; System.out.println("After deleting last element "+lastElement); for(int i=0; i<size-1; i++) { System.out.print(arr[i]+" "); } } }
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 454700 โ€บ java โ€บ Proper-remove-shift-elements-array
Proper way to remove and shift elements of an array (Beginning Java forum at Coderanch)
You can try dissecting ArrayList ... middle of an array: There is no need to do anything with the element deleted. Iterate along the array from 1 after the deleted element, to the last element....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-remove-last-array-element-in-javascript-and-return-it
How to remove last array element in JavaScript and return it?
August 26, 2022 - If we want to remove the first element of an array, use the JavaScript Array.shift() method. Following is the syntax of JavaScript Array pop() Method โˆ’ ... This method does not accept any parameters.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-remove-the-last-element-of-an-array-in-swift
How to remove the last element of an array in Swift
Line 5: We call the removeLast() method on the array. Line 6: We print the modified array because the last element, "Facebook", was removed.
๐ŸŒ
Java Guides
javaguides.net โ€บ 2024 โ€บ 06 โ€บ java-arraylist-removelast-method.html
Java ArrayList removeLast() Method
June 11, 2024 - The ArrayList.removeLast() method in Java 21 provides a convenient way to remove the last element from an ArrayList. By understanding how to use this method, you can efficiently manage the contents of your ArrayList in Java applications.
๐ŸŒ
HackerNoon
hackernoon.com โ€บ how-to-remove-the-last-element-of-a-javascript-array
How to Remove the Last Element of a JavaScript Array | HackerNoon
November 6, 2022 - One of the most frequent operations we perform on an array is removing the last element. There are a few different ways to do this - but one of the most common
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-remove-last-element-from-an-array-in-php
How to remove last element from an array in PHP? - javatpoint
How to remove last element from an array in PHP with examples, php file, php session, php date, php array, php form, functions, time, xml, ajax, php mysql, regex, string, oop, chop(), bin2hex(), addslashes(), addcslashes() etc.
๐ŸŒ
YouTube
youtube.com โ€บ coding simplified
Array - 2: Delete an element from Array | from end | from given position - YouTube
Source Code:https://thecodingsimplified.com/delete-an-element-from-array/In this video, we're going to reveal exact steps to Delete an element from Array in ...
Published ย  October 16, 2017
Views ย  17K