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 OverflowSee 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;
}
Since Java 21, simply using List.removeLast, for example:
List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
System.out.println(list.removeLast()); // 3 - removes and returns the last element
Note: if the list is not empty, the implementation of List.removeLast returns the result of calling remove(size() - 1). Otherwise, it throws NoSuchElementException.
The time complexity of removing the last element from ArrayList is O(1) - it is just decrementing the size of the list by 1 under the hood.
Is there an easy and elegant way of removing the last element of an array?
java - Removing the last element of an ArrayList - Stack Overflow
How does an ArrayList remove an element from an array?
Why is it that removing an element from the start of an array O(n) while at the end O(1)?
Videos
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?
Your test should be like below. In the test code in the original post, you are not actually invoking the method that you are trying to test.
public class UTest
{
@Test
public void testMultipleLast() {
ArrayList<Integer> input = new ArrayList<Integer>(asList(1,1,3,5,7,1,5,9,1));
ArrayList<Integer> result = new ArrayList<Integer>(asList(1,1,3,5,7,1,5,9));
// int x = ?
ArrayList<Integer> actual = SomeClass.removeLastOccurrence(x, input)
assertEquals(result, actual);
}
}
and the removeLastOccurrence() method can do the following
if(list != null && !list.isEmpty()){
list.remove(list.size() - 1);
}
It's because you are not removing any elements.
list.get(list.size()-1);
does not remove elements.
use
list.remove(list.size()-1)
instead.