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.
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
});
It should be:
ClientThread hey = clients.get(clients.size() - 1);
clients.remove(hey);
Or you can do
clients.remove(clients.size() - 1);
The minus ones are because size() returns the number of elements, but the ArrayList's first element's index is 0 and not 1.
The compiler complains that you are trying something of a list of ClientThread objects to a String. Either change the type of hey to ClientThread or clients to List<String>.
In addition: Valid indices for lists are from 0 to size()-1.
So you probably want to write
String hey = clients.get(clients.size()-1);
Since in Java, arrays are non-resizable, you will have to copy everything into a new, shorter array.
Arrays.copyOf(original, original.length-1)
I know its a very old thread. Still the approved answer itself didn't work for me. And this is how I resolved it.
Create a method like this:
String[] sliceArray(String[] arrayToSlice, int startIndex, int endIndex) throws ArrayIndexOutOfBoundsException {
if (startIndex < 0)
throw new ArrayIndexOutOfBoundsException("Wrong startIndex = " + startIndex);
if (endIndex >= arrayToSlice.length)
throw new ArrayIndexOutOfBoundsException("Wrong endIndex = " + endIndex);
if (startIndex > endIndex) { // Then swap them!
int x = startIndex;
startIndex = endIndex;
endIndex = x;
}
ArrayList<String> newArr = new ArrayList<>();
Collections.addAll(newArr, arrayToSlice);
for (int i = 0; i < arrayToSlice.length; i++) {
if (!(i >= startIndex && i <= endIndex)) // If not with in the start & end indices, remove the index
newArr.remove(i);
}
return newArr.toArray(new String[newArr.size()]);
}
Then called it like this:
String lines[] = {"One", "Two", "Three", "Four", "Five"};
lines = sliceArray(lines, 0, 3);
This will result in:
"One", "Two", "Three", "Four"
Now I can slice the array in whichever way I want!
lines = sliceArray(lines, 2, 3);
This will result in:
"Three", "Four"
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.