How to Get Last Element in Array in Java? Last Updated : 24 Dec, 2024 Comments Improve Suggest changes 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. Comment More infoAdvertise with us Next Article How to Get Last Element in Array in Java? S shubhamkquv4 Follow Improve Article Tags : Java Java-Arrays Java-Array-Programs Practice Tags : Java 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 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 Array get() Method in Java The java.lang.reflect.Array.get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Syntax Array.get(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned. in 3 min read How to get slice of a Primitive Array in Java Given a Primitive Array, the task is to get a Slice of this array in Java, using start and ending index. Examples: Input: arr[] = {1, 2, 3, 4, 5}, startIndex = 2, endIndex = 4 Output: {3, 4, 5} Input: arr[] = {1, 2, 3, 4, 5}, startIndex = 0, endIndex = 1 Output: {1, 2}Method 1: Naive Method.Get the 8 min read ArrayDeque getLast() Method in Java The java.util.ArrayDeque.getLast() method in Java is used to retrieve or fetch the last element of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the last element of the deque.Syntax: Array_Deque.getLast() Parameters: The method does not 2 min read How to Add Element at First and Last Position of LinkedList in Java? LinkedList is a part of Collection framework present inside java.util package. This class is an implementation of LinkedList data structure which is a linear data structure where the elements are not stored in a contiguous manner and every element is a separate object with a data field and address f 2 min read Like