How to pop the first element of a list in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        // Check if the list is not empty
        if (!list.isEmpty()) {
            // Remove the first element
            list.remove(0);
        }

        // Print the updated list
        for (int num : list) {
            System.out.print(num + " ");
        }
    }
}



/*
run:

2 3 4 5 

*/

 



answered May 2, 2025 by avibootz
...