Java program to return the last element in an array – Here we written the program in four different ways to find the last element of array in Java along with outputs as well.Get last
Print Last Element – Static Method
Here, our problem statement is to find the last element in the given array. For this, our required inputs are the total number of elements in the array or the size of array and the data values in the array.
Our desired output is the last element among these entered data values in the array.
To read the required inputs, we use the Scanner class. For any primitive datatype, this can be used. We first create an object of Scanner class and then, we invoke the nextInt() method since, all the inputs are of integer type. Then, we read the number of elements and each element of the array from console screen by using the nextInt() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in an Array"); int n=sc.nextInt(); int a[]=new int[n]; System.out.println("Enter "+n+" array elements "); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } |
After getting the inputs, we call the static method (LastElement) and pass the array as argument. To calculate the length of the array, we have a predefined method arrayname.length. So, we make use of this method to find the last element. Since indexing of array starts from zero, the last element would be in the index length-1. So, if the length is zero, it means it has no elements so, we return zero to the main method else, we return the last element i.e., a[length-1]. This returned value is displayed on the console screen using the println() method.
if(a.length==0) {
System.out.println(“Array is Empty”);
return 0;
}
return a[a.length-1];
Here is the Java Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Scanner; class LastElementOfArray { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in an Array"); int n=sc.nextInt(); int a[]=new int[n]; System.out.println("Enter "+n+" array elements "); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } System.out.println("Last elment of an array is "+LastElement(a)); } static int LastElement(int a[]) { if(a.length==0) { System.out.println("Array is Empty"); return 0; } return a[a.length-1]; } } |
Output:
Here, array a[]= { 12, 97, -50}
Length of array = 3
a[0]=12, a[1]=97, a[2]=-50
therefore, a[length-1]=a[3-1]-a[2]=-50 which is the last element.
1 2 3 4 5 6 7 |
Enter the number of elements in an Array 3 Enter 3 array elements 12 97 -50 Last elment of an array is -50 |
Java Getting the Last Three Elements from A List/array List
Instead of using a separate static method, we can write the entire code within main method. But, if this is done then, it is not reusable. So, we first read the total number of elements (n) and the individual elements of array (a).
If the total number of elements (n) is equal to zero then, the array is empty so, we give this warning and stop the execution.
Else, we read the elements in the array and the last element of the array would be at index n-1. Because, the indexing starts from zero so, the last element of an array of length n would be at (n-1)th position i.e., a[n-1]. This value is displayed and we reach the end of execution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; class LastElementOfArray { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in an Array"); int n=sc.nextInt(); if(n==0) { System.out.println("Array is Empty"); return; } int a[]=new int[n]; System.out.println("Enter "+n+" array elements "); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } System.out.println("Last elment of an array is "+a[n-1]); } } |
Output:
1 2 3 4 5 6 7 8 |
java FirstElementOfArray Enter the number of elements in an Array 3 Enter 3 array elements 54 34 2 Last elment of an array is 2 |