how to reverse an array and store it in another array in java code example

Example: Reverse an array without using another array in java

// Reverse an array without using another array in java
import java.util.Scanner;
public class ReverseWithoutArray 
{
      public static void main(String[] args) 
      {
         int number, temp, a;
         Scanner sc = new Scanner(System.in);
         System.out.print("Please enter number of elements in the array: ");
         number = sc.nextInt();
         int[] arrayNumbers = new int[number];
         System.out.println("Please enter " + number + " number elements.");
         for(a = 0; a < number; a++)
         {
             arrayNumbers[a] = sc.nextInt();
         }
         for(a = 0; a < number; a++, number--)
         {
             temp = arrayNumbers[a];
             arrayNumbers[a] = arrayNumbers[number - 1];
             arrayNumbers[number - 1] = temp;
         }
         System.out.println("Reverse of an array is : ");
         for(a = 0; a < arrayNumbers.length; a++)
         {
             System.out.println(arrayNumbers[a]);
         }
         sc.close();
      }
}

Tags:

Java Example