How to Get First Element in Array in Java? Last Updated : 24 Dec, 2024 Suggest changes Share Like Article Like Report In Java, to get the first element in an array, we can access the element at index 0 using array indexing. Example 1: Below is a simple example that demonstrates how to access the element at index 0 in an array. Java public class Geeks { public static void main(String[] args) { // Declare and initialize an array int[] n = {5, 15, 25, 35, 45}; // Access the first element int f = n[0]; // Print the first element System.out.println("" + f); } } Output5 SyntaxarrayName[0];arrayName: The name of the array.[0]: The index of the first element in the array.Example 2: Here, we are trying to access the first element in an empty array. It will throw an ArrayIndexOutOfBoundsException. Java // Handling empty arrays public class Geeks { public static void main(String[] args) { // Declare an empty array int[] n = {}; // Check if the array is empty if (n.length > 0) { System.out.println("The first element is: " + n[0]); } else { System.out.println("The array is empty."); } } } OutputThe array is empty. Example 3: Here, we are accessing the first element in an Array of String. Java // Access the first element // in a String array public class Geeks { public static void main(String[] args) { // Declare and initialize a string array String[] s = {"Cherry", "Strawberry", "Blueberry"}; // Access the first element String f = s[0]; // Print the first element System.out.println(" " + f); } } Output Cherry Advertise with us Next Article How to Get First Element in Array in Java? S shubhamkquv4 Follow Similar Reads How to Get Last Element in Array in Java? In Java, to get the last element in an array, we can access the element at the index array.length - 1 using array indexing. The length property of the array provides its total size, and subtracting one from it gives the index of the last element.Example 1: Here, we will access the last element in an 2 min read Get first and last elements from ArrayList in Java Given an array list, find the first and last elements of it. Examples: 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 h 3 min read ArrayDeque getFirst() Method in Java The java.util.ArrayDeque.getFirst() method in Java is used to retrieve or fetch the first element of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the first element of the deque. Syntax: Array_Deque.getFirst() Parameters: The method doe 2 min read Find first and last element of ArrayList in java Prerequisite: ArrayList in Java Given an ArrayList, the task is to get the first and last element of the ArrayList in Java, Examples: 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 Approach: Get the ArrayList 2 min read Array getInt() Method in Java The java.lang.reflect.Array.getInt() is an inbuilt method in Java and is used to return an element at the given index from the specified Array as a int. Syntax Array.getInt(Object []array, int index) Parameters: This method accepts two mandatory parameters: array: The object array whose index is to 3 min read Article Tags : Java Java-Arrays Java-Array-Programs Practice Tags : Java Like