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 OverflowMOOC Java 03_17 RemoveLast Exercise Help Needed
arraylist - Remove Last Element from a List in Java - Stack Overflow
java - How to remove the last string in an array element using parameters of another method named removeLast() - Stack Overflow
Java* Add the removeLast method to
the ArrayList class of Section 16.4. Return the removed
element or null if the array list is empty.
ArrayList.java
/**
This is a simplified implementation of an array
list.
*/
public class ArrayList
{
private Object[] elements;
private int currentSize;
/**
Constructs an empty array list.
*/
public
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;
}
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.
So I'm just starting the Mooc java course and i'm not sure how to get past this exercise.
Here's my code:
import java.util.ArrayList;
public class RemoveLast {
public static void main(String[] args) {
}
public static void remove (ArrayList<String> list) {
if (!list.isEmpty()) {
int last = list.size() - 1;
list.remove(last);
}
}
}
When I start using the method nothing seems to be wrong, but when I submit it, the fail message tells me "the removeLast method should not cause an exception. Make sure the method doesn't do anything to an empty list."
Thank you :)
tl;dr
pathToFile.getParent()
Path#getParent
No need to resort to crude string manipulation. Java NIO abstracts us away from the platform-specific details.
Call Path#getParent to get another path object while dropping the last element of the original.
Path pathToFile = Paths.get( "SomeFolder" , "SomeFile" ) ;
Path pathToFolder = pathToFile.getParent() ;
List#removeLast
By the way, to answer the title of your Question…
In Java 21+, List is a sub-interface of SequencedCollection. This brings convenient new methods such as removeLast.
myList.removeLast() ;
Actually, you might be getting the UnsupportedOperationException, this is happening because you have used the Arrays.asList method, by using which the list cannot be modified i.e. no elements can be added or removed from the path list.
So, to perform your desired operation, you can use the below code.
String[] pathArray = Paths.get("").toAbsolutePath().toString().split("\\\\");
List<String> path = new ArrayList<>();
Collections.addAll(path, pathArray);
System.out.println(path);
int removeJavaFile = path.size() - 1;
path.remove(removeJavaFile);
System.out.println("Path: "+path);
String filePath = path.stream().map(n -> String.valueOf(n)).collect(Collectors.joining("\\\\"));
System.out.println(filePath);
Here, first you get the pathArray and then add those array elements into a list which can be modified which makes you remove the last element from the list.
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);