How to Access Last Element of Array In Java

How to Access Last Element of Array In Java? In this post, we will discuss how to access last element of array In Java. To do this we can use the inbuilt “length” property defined for the array in the Java programming example. Also see:- Find Length of Array in Java

Let us see some examples of last element of an array In Java:-

1) Array = [1,6,8,5,36]
The last element is 36

2) Array = [6,7,8,0]
The last element is 0

3) Array = [4,5,-9]
The last element is -9

Program to Access Last Element of Array In Java

Step-1:- Import the necessary classes.
Step-2:- Create a class with the main method and the method required to retrieve the last element.

Step-3:- Instantiate the scanner class and take the inputs from the user by using for loop read the elements of the array.
Step-4:- Print the array elements by Arrays.toString() method, this method converts the array elements to string elements.

Step-5:- Print the last element using lastElement() method.
Step-6:- The lastElement() method takes an array as a parameter if the length of an array is 0 it says “Array is Empty” or else it returns the last element.

Demonstration of How to Access Last Element of Array In Java

import java.util.Scanner;
import java.util.Arrays;

public class Main {
   public static void main(String arg[]) {
      Scanner scan = new Scanner(System.in);

      System.out.print("Enter the number of "+
                    "elements in the array: ");
      int n = scan.nextInt();

      int arr[] = new int[n];
      System.out.println("Enter " + n + " array elements: ");
      for (int i = 0; i < n; i++) {
         arr[i] = scan.nextInt();
      }

      System.out.println("Array = " + Arrays.toString(arr));
      System.out.println("The last element of an array is = " 
                          + lastElement(arr));
      scan.close();
   }

   public static int lastElement(int arr[]) {
      if (arr.length == 0) {
         System.out.println("Array is Empty");
         return 0;
      }
      return arr[arr.length - 1];
   }
}

Output:-

Enter the number of elements in the array: 5
Enter 5 array elements:
30 40 15 20 25
Array = [30, 40, 15, 20, 25]
The last element of the array is = 25

Enter the number of elements in the array: 7
Enter 7 array elements:
9 18 27 36 45 54 63
Array = [9, 18, 27, 36, 45, 54, 63]
The last element of the array is = 63

Test case where the array is empty

Enter the number of elements in the array: 0
Enter 0 array elements:
Array = [ ]
Array is Empty
The last element of an array is = 0

In the above case, we have used the Arrays.toString() method to display the array. In Java Arrays class contains multiple in-built method used to solve array related problems like Arrays.sort(), Arrays.copyof(), Arrays.copyOfRange(), Arrays.fill(), Arrays.equals(), and more. Also see:- Different ways to Print Array

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *