Java - Iterate Array in Reverse Order Last Updated : 09 Dec, 2024 Suggest changes Share Like Article Like Report In Java, iterating over an array in reverse order means accessing the elements of the array from the last to the first. We have multiple ways to iterate an array in reverse order.Example 1: The most simplest way to iterate over an array in reverse order is by using a for loop. Java // Java program to iterate array in // reverse order using for loop public class GFG { public static void main(String[] args) { // taking integer array int[] n = { 10, 20, 30, 40, 50 }; System.out.println(""); // using for loop to print array // in reverse order for (int i = n.length - 1; i >= 0; i--) { System.out.print(n[i] + " "); } } } Output50 40 30 20 10 Example 2: The other method is using an enhanced for loop, where we first reverse the array and then iterate directly on the elements. Java // Java Program to iterate over an array // in reverse order using enhanced for loop import java.util.*; public class Main { public static void main(String[] args) { // Array initialization Integer[] arr = {10, 20, 30, 40, 50}; // Reversing the array Collections.reverse(Arrays.asList(arr)); // Iterating over the reversed array for (int n : arr) { System.out.print(n + " "); } } } Output50 40 30 20 10 Example 3: We can also use a while loop to iterate over an array in reverse order. This method is similar to the for loop but uses a condition explicitly. Java // Java program to iterate over an array // in reverse order using while loop public class Main { public static void main(String[] args) { // Array initialization int[] arr = {10, 20, 30, 40, 50}; int i = arr.length - 1; // Iterating over the array in reverse order while (i >= 0) { // Accessing each element of the array System.out.print(arr[i] + " "); i--; } } } Output50 40 30 20 10 Example 4: We can also use the Streams API in Java 8 and later, which provides a concise way to iterate over an array in reverse order. Java // Java Program to iterate over an array // in reverse order using Streams API import java.util.stream.IntStream; public class Main { public static void main(String[] args) { // Array initialization int[] arr = {10, 20, 30, 40, 50}; // Iterating over the array in reverse // order using Streams IntStream.range(0, arr.length) .map(i -> arr[arr.length - i - 1]) .forEach(i -> System.out.print(i + " ")); } } Output50 40 30 20 10 Advertise with us Next Article Java - Iterate Array in Reverse Order J juhisrivastav Follow Similar Reads Reverse an Array in Java Reversing an Array is a common task in every programming language. In Java, there are multiple ways to reverse an array. We can reverse it manually or by using built-in Java methods. In this article, we will discuss different methods to reverse an array with examples.Let us first see the most common 4 min read Integer reverse() Method In Java The java.lang.Integer.reverse() is an inbuilt method in Java and is used to return the reverse order of the bits in the two's complement binary representation of the specified int value. Syntax: public static int reverse(int a) Parameters: The parameter a is an integer value whose bits are to be rev 2 min read ArrayDeque iterator() Method in Java The Java.util.ArrayDeque.iterator() method is used to return an iterator of the elements of the ArrayDeque. Syntax: Iterator iterate_value = Array_Deque.iterator(); Parameters: The method does not take any parameter. Return Value: The method iterates over the elements of the deque and returns the va 2 min read How to create a TreeMap in reverse order in Java By default TreeMap elements in Java are sorted in ascending order of keys. However, we can create the TreeMap in reverse order using Collections.reverseOrder() method in Java and display the elements in descending order of keys. The Collections.reverseOrderS() method in Java returns a Comparator tha 2 min read Reverse an ArrayList in Java using ListIterator Assuming you have gone through arraylist in java and know about arraylist. This post contains different examples for reversing an arraylist which are given below:1. By writing our own function(Using additional space): reverseArrayList() method in RevArrayList class contains logic for reversing an ar 6 min read Article Tags : Java Java-Arrays Practice Tags : Java Like