How do I reverse an int array in Java

Java 8Object Oriented ProgrammingProgramming

Following program reverses an int array.

Example

public class Tester {
   public static void main(String[] args) {
      int[] numbers = {1,2,3,4,5};

      //swap the numbers till the midpoint comes
      for (int start = 0, end = numbers.length - 1;
      start <= end;
      start++, end--) {
         int temp = numbers[start];
         numbers[start]=numbers[end];
         numbers[end]=temp;
      }
      for(int number: numbers){
         System.out.print(number + " ");
      }
   }
}

Output

5 4 3 2 1
raja
Ali
Published on 06-Feb-2018 08:56:31
Advertisements