To reverse an int array, you swap items up until you reach the midpoint, like this:
for(int i = 0; i < validData.length / 2; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
The way you are doing it, you swap each element twice, so the result is the same as the initial list.
Answer from 3lectrologos on Stack Overflow Top answer 1 of 16
357
To reverse an int array, you swap items up until you reach the midpoint, like this:
for(int i = 0; i < validData.length / 2; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
The way you are doing it, you swap each element twice, so the result is the same as the initial list.
2 of 16
338
With Commons.Lang, you could simply use
ArrayUtils.reverse(int[] array)
Most of the time, it's quicker and more bug-safe to stick with easily available libraries already unit-tested and user-tested when they take care of your problem.
Videos
01:00
Java Program to Reverse an Array without Additional Array | Java ...
05:50
How To Reverse An Array In Java Explained With Debugging | Java ...
09:45
Java Program to Reverse an Array|How to Reverse an Array in Java ...
03:41
Reversing the Values in an Array (Java Tutorial) - YouTube
07:39
Java Program to Reverse an Array - YouTube
11:04
How to reverse elements in an array in java? - YouTube
Scaler
scaler.com โบ home โบ topics โบ reverse an array in java
Reverse an Array in Java - Scaler Topics
April 28, 2024 - In the above example, the array A1 contains the elements [1, 2, 3, 4, 5]. On reversing this array, we get an array A2, i.e., [5, 4, 3, 2, 1]. Note how the element 3, present in the middle of the array, remains in place.
Coderanch
coderanch.com โบ t โบ 571086 โบ java โบ Reverse-array-java
Reverse an array in java (Beginning Java forum at Coderanch)
Output of my program: 7 random digits myArray[0] = 5 myArray[1] = 14 myArray[2] = 88 myArray[3] = 18 myArray[4] = 20 myArray[5] = 8 myArray[6] = 46 myArray[7] = 100 After reversing the array myArray[0] = 100 myArray[1] = 46 myArray[2] = 8 myArray[3] = 20 myArray[4] = 20 myArray[5] = 8 myArray[6] ...
TutorialsPoint
tutorialspoint.com โบ home โบ javaexamples โบ java arrays reverse example
Java Arrays Reverse Example
September 1, 2008 - Following example reverses an array list by using Collections.reverse(ArrayList)method. import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) { ArrayList arrayList = new ArrayList(); arrayList.add("A"); arrayList.add("B"); arrayList.add("C"); arrayList.add("D"); arrayList.add("E"); System.out.println("Before Reverse Order: " + arrayList); Collections.reverse(arrayList); System.out.println("After Reverse Order: " + arrayList); } }
Java Code Geeks
examples.javacodegeeks.com โบ home โบ java development โบ core java
Reverse Array Java Example - Java Code Geeks
February 7, 2022 - You can reverse an array by converting an array to ArrayList and then reverse the ArrayList or use the Apache Commons ArrayUtils.reverse().
GeeksforGeeks
geeksforgeeks.org โบ java โบ java-iterate-array-in-reverse-order
Java - Iterate Array in Reverse Order - GeeksforGeeks
July 23, 2025 - // 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 ...
Upgrad
upgrad.com โบ home โบ tutorials โบ software & tech โบ reverse an array in java
Comprehensive Guide to Reversing an Array in Java
April 1, 2025 - Array Reversal is the process of taking an array (a list of elements) and flipping the order of its contents. Let's take an example, if you have an array(1,2,3,4,5), reversing it results: in (5,4, 3,2,1).
takeuforward
takeuforward.org โบ data-structure โบ reverse-a-given-array
Reverse a given Array - Tutorial
Search for a command to run
Java67
java67.com โบ 2016 โบ 10 โบ 3-ways-to-reverse-array-in-java-coding-interview-question.html
[Solved] 3 Examples to reverse an Array in Java - Example Tutorial | Java67
This is another simple way to reverse an array in Java is by first converting the array to List and then using the Collections.reverse() method to reverse the ArrayList. This method takes a List and reverses the element in linear time. You should use this approach if you need to reverse an array in your project. You can reverse int, String, or any type of array by using this method. Let's see an example of reversing a String array in Java:
STechies
stechies.com โบ reverse-array-java
3 Methods to Reverse an Array in Java
This particular method of array reverse in Java converts the given array into an ArrayList and then utilizes the Collections.reverse() method that takes up the items in the list and reverses the elements in linear time. Arrays of int, string or other types can be reversed with the help of this method.The following example depicts a reversal of a string type array in Java:
Javatpoint
javatpoint.com โบ java-program-to-print-the-elements-of-an-array-in-reverse-order
Java Program to print the elements of an array in reverse order - Javatpoint
Java Program to print the elements of an array in reverse order - Java Program to print the elements of an array in reverse order on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.
BeginnersBook
beginnersbook.com โบ 2017 โบ 09 โบ java-program-to-reverse-the-array
Java Program to reverse the Array
On the second * iteration i is ... last. */ j = i - 1; i = 0; scanner.close(); while(i<j) { temp = number[i]; number[i] = number[j]; number[j] = temp; i++; j--; } System.out.print("Reversed array: "); for(i=0; i<counter; i++) { System.out.print(number[i]+ " "); } } }...
Medium
medium.com โบ @AlexanderObregon โบ reversing-the-order-of-an-array-with-java-loops-95b3546e023b
Reversing the Order of an Array with Java Loops | Medium
September 7, 2025 - Each call to data[index] shifts from the start of the array to the right position. Nothing gets copied during this read, the runtime just calculates the offset and fetches the value. Reversing depends on the same principle, except the offsets are taken in the opposite order. It helps to remember that Java arrays store their length as part of their structure.
Java67
java67.com โบ 2016 โบ 01 โบ java-program-to-reverse-array-in-place.html
How to Reverse an Array in place in Java? Example Solution | Java67
In the last section, I have explained to you the logic or algorithm to reverse an array in place, now is the time to convert that algorithm into pseudo-code and then real code in Java. You will also calculate the time and space complexity of our solution, which is often required in Interviews as well as in the real world to meet your performance SLA. So, let's see an example of how you can reverse an array of String in Java in place.