java pop the last item of arraylist code example
Example 1: How to remove last element from arraylist in java
import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveLastElement
{
public static void main(String[] args)
{
List<Integer> al = new ArrayList<>();
al.add(56);
al.add(28);
al.add(39);
al.add(59);
al.add(82);
int index = al.size() - 1;
al.remove(index);
System.out.println("After remove last element from arraylist: " + al);
}
}
Example 2: java remove last array element
Arrays.copyOf(original, original.length-1)