/** * @author lautturi.com * Java example: how to get first and last element in an array in java */ import java.util.*; import java.util.Map.Entry; public class Lautturi { public static void main(String[] args) { int[] numbers = { 1,12,13,21,27,36,38,43,59,106 }; int firstElement = numbers[0]; int lastElement = numbers[numbers.length-1]; System.out.println("The first element of array is:"+firstElement); System.out.println("The last element of array is:"+lastElement); } }
output:
The first element of array is:1 The last element of array is:106