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.
apex - How to remove last element from a List<String> - Salesforce Stack Exchange
java - IndexOutOfBoundsException when removing the last element of a list using list.remove(list.size()) - Stack Overflow
java - Removing the last element of an ArrayList - Stack Overflow
java - How to remove last element of arraylist - Stack Overflow
List indices go from 0 to list.size() - 1. Exceeding the upper bound results in the IndexOutOfBoundsException
list.remove(list.size() - 1);
List#removeLast
As of Java 21, you can now use removeLast().
Removes and returns the last element of this collection (optional operation).
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/List.html#removeLast()
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.
You are getting IndexOutOfBounds because you are trying to remove an item that isn't there, so you should perform a check on your removal to stop the removal if there is nothing there:
if(array.size() > 0){
array.remove(array.size() -1);
}
You have an empty array as @TheAndroidDev said. What about to use Rx, something like this:
Observable.from(array)
.interval(10, TimeUnit.MINUTES)
.map(i -> array.size() > 0 ? array.remove(array.size() - 1) : null)
.take(array.size())
.subscribe(integer -> {
// Do something with the integer or type you use
});
You could consider using the subList(...) method of ArrayList.
You can create a while-loop, like this:
List yourList = ...; // Your list
int removed = 0; // Setup the variable for removal counting
while (removed < Math.min(secondList.size(), 5)) { // While we still haven't removed 5 entries OR second list size
yourList.remove(yourList.size() - 1); // Remove the last entry of the list
removed++; // Increases 'removed' count
}