Remove first element from ArrayList in Java

Given an ArrayList collection in Java, the task is to remove the first element from the ArrayList.

Example:

Input: ArrayList[] = [10, 20, 30, 1, 2]
Output: [20, 30, 1, 2]
After removing the first element 10, the ArrayList is:
[20, 30, 1, 2]

Input: ArrayList[] = [1, 1, 2, 2, 3]
Output: [1, 2, 2, 3]
After removing the first element 1, the ArrayList is:
[1, 2, 2, 3]

We can use the remove() method of ArrayList container in Java to remove the first element.

ArrayList provides two overloaded remove() method:

  • remove(int index) : Accept index of the object to be removed. We can pass the first element’s index to the remove() method to delete the first element.
  • remove(Object obj) : Accept object to be removed. If the ArrayList does not contain duplicates, we can simply pass the first element value as an object to be deleted to the remove() method, and it will delete that value.

    Note: Incase the ArrayList contains duplicates, it will delete the first occurrence of the object passed as a parameter to the remove() method.

Below is the implementation to delete the first element using the two approaches:



  • Program 1: Using remove(int index). Index of elements in an ArrayList starts from zero. Therefore, index of first element in an ArrayList is 0.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to delete the first element of ArrayList
    import java.util.List;
    import java.util.ArrayList;
      
    public class GFG {
        public static void main(String[] args)
        {
            List<Integer> al = new ArrayList<>();
            al.add(10);
            al.add(20);
            al.add(30);
            al.add(1);
            al.add(2);
      
            // First element's index is always 0
            int index = 0;
      
            // Delete first element by passing index
            al.remove(index);
      
            System.out.println("Modified ArrayList : " + al);
        }
    }

    chevron_right

    
    

    Output:

    Modified ArrayList : [20, 30, 1, 2]
    
  • Program 2: Using remove(Object obj).
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to delete first element of ArrayList
    import java.util.List;
    import java.util.ArrayList;
      
    public class GFG {
        public static void main(String[] args)
        {
            List<Integer> al = new ArrayList<>();
            al.add(10);
            al.add(20);
            al.add(30);
            al.add(1);
            al.add(2);
      
            // Since all elements are unique, pass the first
            // elements value to delete it
            // Note: values are integer object
            al.remove(new Integer(10));
      
            System.out.println("Modified ArrayList : " + al);
        }
    }

    chevron_right

    
    

    Output:

    Modified ArrayList : [20, 30, 1, 2]
    

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Senior Technical Content Engineer | GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.