How to Get Last Element in Array in Java? Last Updated : 24 Dec, 2024 Suggest changes Share Like Article Like Report 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 Integer array. Java // Get the last element // in an integer array public class Geeks { public static void main(String[] args) { // Declare and initialize an array int[] arr = {1, 2, 3, 4, 5}; // Access the last element int l = arr[arr.length - 1]; // Print the last element System.out.println("" + l); } } Output5 SyntaxarrayName[arrayName.length - 1];arrayName: The name of the array.length: The total number of elements in the array.- 1: Subtracting one gives the index of the last element.Example 2: Here, we will access the last element in an array of Strings. Java // Access the last 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 last element String l = s[s.length - 1]; // Print the last element System.out.println("" + l); } } OutputBlueberry Example 3: If we try to access the last element of 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[] arr = {}; // Check if the array is empty if (arr.length > 0) { System.out.println("" + arr[arr.length - 1]); } else { System.out.println("The array is empty."); } } } OutputThe array is empty. Advertise with us Next Article How to Get Last Element in Array in Java? S shubhamkquv4 Follow Similar Reads How to Get First Element in Array in Java? 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.Javapublic class Geeks { public static void main(String[] args) { // Declare and initializ 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 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 Array getLong() Method in Java The java.lang.reflect.Array.getLong() is an inbuilt method in Java and is used to return an element at the given index from a specified Array as a long. Syntax: Array.getLong(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is 3 min read Array getShort() method in Java The java.lang.reflect.Array.getShort() is an in-built method of Array class in Java and is used to return the element present at a given index from the specified Array as a short. Syntax: Array.getShort(Object []array,int index) Parameters: array: The object array whose index is to be returned. inde 3 min read Article Tags : Java Java-Arrays Java-Array-Programs Practice Tags : Java Like