Prerequisite: ArrayList in Java
Given an ArrayList, the task is to get the first and last element of the ArrayList in Java,
Input: ArrayList = [1, 2, 3, 4] Output: First = 1, Last = 4 Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: First = 12, Last = 89
- Get the ArrayList with elements.
- Get the first element of ArrayList with use of get(index) method by passing index = 0.
- Get the last element of ArrayList with use of get(index) method by passing index = size – 1.
Below is the implementation of the above approach:
ArrayList: [1, 2, 3, 4] First : 1, Last : 4
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.
- Remove first element from ArrayList in Java
- Removing last element from ArrayList in Java
- How to remove an element from ArrayList in Java?
- How to find the length or size of an ArrayList in Java
- ArrayList of ArrayList in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.Arraylist.indexOf() in Java
- Java.util.ArrayList.add() Method in Java
- Arraylist.contains() in Java
- ArrayList in Java
- Find the last element of a Stream in Java
- Find the first element of a Stream in Java
- ArrayList isEmpty() in Java with example
- ArrayList trimToSize() in Java with example
- Array of ArrayList in Java
- ArrayList vs. HashMap in Java
- Vector vs ArrayList in Java
- Synchronization of ArrayList in Java
- Arraylist lastIndexOf() in Java with example
- Initialize an ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.