Get first and last elements from ArrayList in Java

Given an array list, find first and last elements of it.

Input : aList = {10, 30, 20, 14, 2}
Output : First = 10, Last = 2

Input : aList = {10, 30, 40, 50, 60}
Output : First = 10, Last = 60


The last element is at index, size – 1 and the first element is stored at index 0. If we know how to get the size of ArrayList then we can get those two values easly. But remember, that you need to use size() method for ArrayList not length, length is used to get the length of array.

Find First and Last in ArrayList:

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program print first and last element of a List
import java.util.ArrayList;
import java.util.List;
public class FindFirstLast {
    public static void getFirstLat(List<Integer> list)
    {
  
        // Displaying ArrayList elements
        System.out.println("ArrayList contains: " + list);
  
        // Logic to get the last element from ArrayList
        if (list != null && !list.isEmpty()) {
            System.out.println("First element is: "
                               + list.get(0));
            System.out.println("Last element is: "
                               + list.get(list.size() - 1));
            return;
        }
    }
  
    public static void main(String[] args)
    {
        /* Creating ArrayList of Integer and adding
          elements to it */
        List<Integer> al = new ArrayList<Integer>();
        al.add(3);
        al.add(1);
        al.add(4);
        al.add(5);
        al.add(2);
  
        getFirstLat(al);
    }
}

chevron_right


Output:

ArrayList contains: [3, 1, 4, 5, 2]
First element is: 3
Last element is: 2

Application : The first element is your lowest and the last element is your highest in case of ascending order and opposite then the first element would be the maximum and last element would be the minimum if List is in descending order.

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program print Maximum and Minmum Value of a
// sorted List, List may be increasing or decreasing order
import java.util.ArrayList;
import java.util.List;
public class FindFirstLast {
    // function find and print Maximum and Minmum value
    public static void getFirstLat(List<Integer> list)
    {
  
        // Displaying ArrayList elements
        System.out.println("ArrayList contains: " + list);
  
        // Logic to get the last element from ArrayList
        if (list != null && !list.isEmpty()) {
            if (list.get(0) < list.get(list.size() - 1)) {
  
                // if list in increasing order
                System.out.println("Minimum Value: "
                                   + list.get(0));
                System.out.println("Maximum Value: "
                           + list.get(list.size() - 1));
                return;
            }
  
            else {
  
                // if list in decreasing order
                System.out.println("Minimum Value: "
                            + list.get(list.size() - 1));
                System.out.println("Maximum Value: "
                                   + list.get(0));
                return;
            }
        }
    }
  
    public static void main(String[] args)
    {
        /* Creating ArrayList of Integer and adding
            elements to it */
        List<Integer> al = new ArrayList<Integer>();
        al.add(5);
        al.add(4);
        al.add(3);
        al.add(2);
        al.add(1);
  
        getFirstLat(al);
    }
}

chevron_right


Output:

ArrayList contains: [5, 4, 3, 2, 1]
Minimum Value: 1
Maximum Value: 5


My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

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.