Reverse an array without using another array in java

Let’s learn to reverse an array without using another array in java.

Reverse an array without using another array in java

Here we are going to reverse an array without using another array.

Here’s java program.

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();
      }
}


Output:

Please enter number of elements in the array: 10
Please enter 10 number elements.
1
2
3
4
5
6
7
8
9
10
Reverse of an array is :
10
9
8
7
6
5
4
3
2
1


Also read – ArrayList in java