How to remove the first element of an ArrayList in Java
In this tutorial, we are going to learn about how to remove the first element of an ArrayList in Java.
Consider, we have a following ArrayList:
List<Integer> prices = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
To remove the first element of a ArrayList, we can use the list.remove()
method by passing its index 0
as an argument to it.
0 is the index of an first element.
Here is an example, that removes the first element 1
from the prices
ArrayList:
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
public class Main
{
public static void main(String[] args) {
List<Integer> prices = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
prices.remove(0);
System.out.println(prices);
}
}
Output:
[2,3,4]