How to get first and last elements from ArrayList in Java?



ArrayList is a part of the Java Collections Framework. Which is a dynamic type of array that can grow and shrink as needed. It is a resizable array implementation of the List interface. The ArrayList class is used when we want to store a list of elements in a dynamic array.

In this article, let's learn how to get the first and last elements from an ArrayList in Java.

The get() method of the ArrayList class accepts an integer representing the index value and returns the element of the current ArrayList object at the specified index.

Therefore, if you pass 0 to this method, you can get the first element of the current ArrayList, and if you pass list.size()-1, you can get the last element.

Let's look at the steps:

  • Declare and initialize an ArrayList object.
  • Use the get(0) method to get the first element of the ArrayList.
  • Then get(list.size()-1) method to get the last element of the ArrayList.

Example

Following is the Java program to get the first and last elements from an ArrayList:

import java.util.ArrayList;
public class GetTheFirstandLastEl{
   public static void main(String[] args){
      ArrayList<String> list = new ArrayList<>();
      list.add("Banana");
      list.add("Apple");
      list.add("Mango");
      list.add("Orange");
      list.add("Grapes");
      list.add("Pineapple");
      String firstElement = list.get(0);
      String lastElement = list.get(list.size()-1);
      System.out.println("First element: " + firstElement);
   }
}

Output

Following is the output of the above code:

First element: Banana
Last element: Pineapple

Getting the first and last elements using the for loop

We can also use the for loop to get the first and last elements from an ArrayList.

In this method, we will use the for loop to iterate through the ArrayList and get the first and last elements.

Let's look at the steps:

  • Declare and initialize an ArrayList object.
  • Use the for loop to iterate through the ArrayList.
  • Check if the current index is 0, if it is, assign the current element to the first element variable.
  • Check if the current index is equal to the size of the ArrayList - 1, if it is, assign the current element to the last element variable.

Example

Following is the Java program to get the first and last elements from an ArrayList using the for loop:

import java.util.ArrayList;
public class GetTheFirstandLastEl{
   public static void main(String[] args){
      ArrayList<String> list = new ArrayList<>();
      list.add("Banana");
      list.add("Apple");
      list.add("Mango");
      list.add("Orange");
      list.add("Grapes");
      list.add("Pineapple");
      String firstElement = "";
      String lastElement = "";
      for(int i=0; i<list.size(); i++){
         if(i == 0){
            firstElement = list.get(i);
         }
         if(i == list.size()-1){
            lastElement = list.get(i);
         }
      }
      System.out.println("First element: " + firstElement);
   }
}

Output

Following is the output of the above code:

First element: Banana
Last element: Pineapple
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-09-01T12:48:08+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements