Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Arrays; 
 public class Javaexcercise {
 public static void main(String[] args)
 {
    int[] array_nums = {10, 20, 30};
    System.out.println("Original Array: "+Arrays.toString(array_nums)); 
    int x = array_nums[0];
    array_nums[0] = array_nums[array_nums.length-1];
    array_nums[array_nums.length-1] = x;
    System.out.println("New array after swaping the first and last elements: "+Arrays.toString(array_nums));  
 }
}

Result:

Original Array: [10, 20, 30]

New array after swaping the first and last elements: [30, 20, 10]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java programming language Basic programs with examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to count the number of even a... >>
<< Write a Java program to compute the sum of the fir...