java for loop loop array backwards code example

Example 1: Reverse an array in java using for loop

// Reverse an array in java using for loop
public class ReverseArrayUsingForLoop 
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = new int[]{2, 4, 6, 8, 10};  
      System.out.println("Given array: ");  
      for(int a = 0; a < arrNumbers.length; a++)
      {
         System.out.print(arrNumbers[a] + " ");
      }
      System.out.println("Reverse array: ");
      // looping array in reverse order
      for(int a = arrNumbers.length - 1; a >= 0; a--) 
      {  
         System.out.print(arrNumbers[a] + " ");  
      }
   }
}

Example 2: how to iterate through an array backwards java

for (int counter = myArray.length - 1; counter >= 0; counter--) {

Tags:

Java Example