- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Get First and Last Elements from an Array List
In this article, we will understand how to get first and last elements from an array list. A list is an ordered collection that allows us to store and access elements sequentially. It contains the index-based methods to insert, update, delete and search the elements. It can also have the duplicate elements.
Below is a demonstration of the same −
Suppose our input is −
Input list: [40, 60, 150, 300]
The desired output would be −
The first element is : 40 The last element is : 300
Algorithm
Step 1 - START Step 2 - Declare ArrayList namely input_list. Step 3 - Define the values. Step 4 - Check of the list is not an empty list. Use the .get(0) and .get(list.size - 1) to get the first and last elements. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.util.ArrayList; public class Demo { public static void main(String[] args){ ArrayList<Integer> input_list = new ArrayList<Integer>(5); input_list.add(40); input_list.add(60); input_list.add(150); input_list.add(300); System.out.print("The list is defined as: " + input_list); int first_element = input_list.get(0); int last_element = input_list.get(input_list.size() - 1); System.out.println("\nThe first element is : " + first_element); System.out.println("The last element is : " + last_element); } }
Output
The list is defined as: [40, 60, 150, 300] The first element is : 40 The last element is : 300
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
import java.util.ArrayList; public class Demo { static void first_last(ArrayList<Integer> input_list){ int first_element = input_list.get(0); int last_element = input_list.get(input_list.size() - 1); System.out.println("\nThe first element is : " + first_element); System.out.println("The last element is : " + last_element); } public static void main(String[] args){ ArrayList<Integer> input_list = new ArrayList<Integer>(5); input_list.add(40); input_list.add(60); input_list.add(150); input_list.add(300); System.out.print("The list is defined as: " + input_list); first_last(input_list); } }
Output
The list is defined as: [40, 60, 150, 300] The first element is : 40 The last element is : 300
- Related Articles
- Get first and last elements from Java LinkedList
- Get first and last elements from Vector in Java
- How to get first and last elements from ArrayList in Java?
- Get first and last elements of a list in Python
- Python program to interchange first and last elements in a list
- C# Program to get the first three elements from a list
- C# program to get the last element from an array
- Program to get the last element from an array using C#
- Get the first and last item in an array using JavaScript?
- Get last N elements from given list in Python
- Java Program to Remove Duplicates from an Array List
- C# program to create a List with elements from an array
- Java Program to Interchange Elements of First and Last in a Matrix Across Rows
- Java Program to Interchange Elements of First and Last in a Matrix Across Columns
- Java Program to Add Element at First and Last Position of a Linked list

Advertisements