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 - Salesforce Stack Exchange
java - IndexOutOfBoundsException when removing the last element of a list using list.remove(list.size()) - Stack Overflow
Java - remove last known item from ArrayList - Stack Overflow
java - How to delete the last element from an array? - Stack Overflow
Videos
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()
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"