Reverse Array in Java W3Schools Guide: Learn Methods and Examples

Robert Jul 13, 2026

Reverse an array in Java? Sure, we can achieve this in a simple yet effective way. Writing an algorithm that traverses the array from the last element to the first and swaps elements as it goes is one approach, but let's discuss more elegant solutions as well. Let's dive right in, using W3Schools as our guide to understand this better.

What are Arrays in Java?
What are Arrays in Java?

Before we begin, recall that in Java, an 'array' is an object that holds multiple values of a specific data type, much like Python's lists but with type constraints. Now, let's explore how to reverse an array in Java.

How to Declare Array in Java – Use, Creating, Types, and Class
How to Declare Array in Java – Use, Creating, Types, and Class

Using the Reverse Order Method

One of the most straightforward ways to reverse an array in Java is by traversing it in reverse order from the last element (length - 1) to the first element (0). This can be done using a loop that iterates in reverse order.

Array Programs in Java | Important Questions for Exams
Array Programs in Java | Important Questions for Exams

Here's a simple code snippet demonstrating this approach:

```java public class Main { public static void main(String[] args) { int[] array = new int[]{1, 2, 3, 4, 5}; for (int i = array.length - 1; i >= 0; i--) { System.out.print(array[i] + " "); } } } ```

Traversing Array in Reverse Order

java array operations
java array operations

In this loop, the index starts at the last element (`array.length - 1`) and ends at the first element (`i >= 0`). The loop decrements the value of `i` on each iteration, effectively moving from the end to the beginning of the array.

Running this code will output: `5 4 3 2 1`, which is the reversed order of the original array.

Using Built-in Array Reverse Function

1D Array in Java Explained | Java Array Basics for Beginners
1D Array in Java Explained | Java Array Basics for Beginners

Java provides a built-in function `Arrays.reverse` that can reverse an array in place, which means it doesn't create a new array and preserve the original array's type and memory. Here's how you can use it:

```java import java.util.Arrays; public class Main { public static void main(String[] args) { Integer[] array = new Integer[]{1, 2, 3, 4, 5}; Arrays.reverse(array); System.out.println(Arrays.toString(array)); // [5, 4, 3, 2, 1] } } ```

The code snippet imports the `Arrays` class and uses the `reverse` function to reverse the `array`. The `toString` function of the `Arrays` class is used to print the reversed array.

Reverse an Array Using API

Java Arrays Cheat Sheet (2026) | Complete Java Arrays Guide for Beginners & Students |
Java Arrays Cheat Sheet (2026) | Complete Java Arrays Guide for Beginners & Students |

If you're looking to reverse a list, remember that you can't reverse an array directly using Java API. Instead, consider converting your array to a list, reversing the list, and then converting it back to an array.

Here's an example:

an image of a computer screen with the words and numbers on it, including two arrows
an image of a computer screen with the words and numbers on it, including two arrows
an open book with instructions on how to use the array array in windows and linux
an open book with instructions on how to use the array array in windows and linux
This One Mistake Makes Your Array Code Slow
This One Mistake Makes Your Array Code Slow
Example of Java Array
Example of Java Array
How to Reverse A String in JavaScript
How to Reverse A String in JavaScript
Java Patterns Notes 1
Java Patterns Notes 1
Arrays in Java Explained | Java Programming Basics for Beginners
Arrays in Java Explained | Java Programming Basics for Beginners
an image of a computer screen with the text traversing a 2d array on it
an image of a computer screen with the text traversing a 2d array on it
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Multidimensional Array in Java (2D Array) - Scientech Easy
Multidimensional Array in Java (2D Array) - Scientech Easy
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
Array initializer in Java
Array initializer in Java
a page in a computer program with text
a page in a computer program with text
an image with the text in blue and green, on top of a dark background
an image with the text in blue and green, on top of a dark background
Common Array Methods
Common Array Methods
Free Java Programming Book
Free Java Programming Book
Arrays in Java | Array Declaration - Scientech Easy
Arrays in Java | Array Declaration - Scientech Easy
How to Reverse a number in Java
How to Reverse a number in Java

```java import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { Integer[] array = new Integer[]{1, 2, 3, 4, 5}; List list = Arrays.asList(array); // Convert the array to a List Collections.reverse(list); // Reverse the list array = list.toArray(new Integer[0]); // Convert the list back to an array System.out.println(Arrays.toString(array)); // [5, 4, 3, 2, 1] } } ```

Finally, remember, practice makes perfect! Once you're comfortable reversing arrays, try tweaking the code to reverse an array of different data types or use advanced Java features to accomplish the same results in different ways.