Write a Java program to swap the first and last elements of an array and create a new array

Program

import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {
       
        int[] array = {20, 30, 40,90,60,10};
        System.out.println("Original Array Elements: " + Arrays.toString(array)); 
        int x = array[0];
        array[0] = array[array.length - 1];
        array[array.length - 1] = x;
        
        System.out.println("New array after swapping the first and last elements: " + Arrays.toString(array));  
    }
}

Output:

Original Array Elements: [20, 30, 40, 90, 60, 10]

New array after swapping the first and last elements: [10, 30, 40, 90, 60, 20]

For latest job updates join Telegram Channel: https://t.me/sateeshm

Programs