Implement a method below which deletes all but the last element from a given list.
import java.util.ArrayList;
import java.util.List;
public class MyClass {
public static void allButLast(List<String> list)
{
List<String> set = new ArrayList<String>();
for (int k : list)
{
set.remove(0, -2);
}
return set;
}
I just started programming and this is a practice question and I was wondering if this type of solution would make sense. I started from the first array block(0) and it stops at the the array block before the last element(-2) and removes it, only keeping the last element and returning it at the end.