
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to get the first element of the List in Java?
The List interface extends Collection interface. It is a collection that stores a sequence of elements. ArrayList is the most popular implementation of the List interface. User of a list has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable.
List interface provides a get() method to get the element at particular index. You can specify index as 0 to get the first element of the List. In this article, we're exploring get() method usage via multiple examples.
Syntax
E get(int index)
Returns the element at the specified position.
Parameters
index - index of the element to return.
Returns
The element at the specified position.
Throws
IndexOutOfBoundsException - If the index is out of range (index < 0 || index >= size())
Example 1
Following is the example showing how to get the first element from a List.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(4,5,6)); System.out.println("List: " + list); // First element of the List System.out.println("First element of the List: " + list.get(0)); } }
Output
This will produce the following result −
List: [4, 5, 6] First element of the List: 4
Example 2
Following is the example where getting the first element from a List can throw an exception.
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); System.out.println("List: " + list); try { // First element of the List System.out.println("First element of the List: " + list.get(0)); } catch(Exception e) { e.printStackTrace(); } } }
Output
This will produce the following result −
List: [] java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at com.tutorialspoint.CollectionsDemo.main(CollectionsDemo.java:11)
- Related Articles
- Get the element ordered first in Java TreeSet
- How to get First Element of the Tuple in C#?
- How to get the first element of an array in PHP?
- Python How to get the last element of list
- Get the first element from a Sorted Set in Java
- How do you get the index of an element in a list in Java?
- How to get the last element of a list in Python?
- Get the first element of array in JavaScript
- How to pop the first element from a C# List?
- How to get the input value of the first matched element using jQuery?
- Get first element with maximum value in list of tuples in Python
- How to get first element in android ConcurrentLinkedDeque?
- How to get the second-to-last element of a list in Python?
- How to display the first element in a JComboBox in Java
- How to get sublist of List in Java?
