Arrays Reverse
## Java Array Reversal: Methods and Examples
Reversing an array is a fundamental operation in Java programming. It involves rearranging the elements of an array so that the first element becomes the last, the second becomes the second-to-last, and so on.
This tutorial covers three common approaches to reversing an array in Java:
1. **Using an Auxiliary Array** (Non-destructive, out-of-place)
2. **Using the Two-Pointer Swap Method** (In-place, memory-efficient)
3. **Using the Java Collections Framework** (Clean, high-level API)
---
## Method 1: Reversing Using an Auxiliary Array
This approach creates a new array of the same size and populates it with elements from the original array in reverse order.
### How It Works
* A new array `b` is initialized with the same length as the original array `a`.
* We iterate through the original array from the beginning (`i = 0`) and place each element into the new array starting from the end (`j - 1`).
* This method is **out-of-place**, meaning the original array remains unmodified.
### Code Example
```java
public class ArrayReverseAuxiliary {
/* Reverse the array using an auxiliary array */
static void reverse(int a[], int n) {
int[] b = new int;
int j = n;
for (int i = 0; i < n; i++) {
b = a;
j = j - 1;
}
/* Print the reversed array */
System.out.println("Reversed array is: \n");
for (int k = 0; k < n; k++) {
System.out.println(b);
}
}
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
reverse(arr, arr.length);
}
}
```
### Output
```text
Reversed array is:
50
40
30
20
10
```
---
## Method 2: Reversing In-Place (Two-Pointer Swap)
This is the most optimal way to reverse an array because it does not require allocating extra memory for a new array. It modifies the array **in-place**.
### How It Works
* We iterate up to the midpoint of the array (`n / 2`).
* In each iteration, we swap the element at index `i` with the corresponding element from the end of the array at index `n - i - 1`.
* A temporary variable `t` is used to facilitate the swap.
### Code Example
```java
public class ArrayReverseInPlace {
/* Reverse the array by swapping elements from both ends */
static void reverse(int a[], int n) {
int i, k, t;
for (i = 0; i < n / 2; i++) {
t = a;
a = a;
a = t;
}
/* Print the reversed array */
System.out.println("Reversed array is: \n");
for (k = 0; k < n; k++) {
System.out.println(a);
}
}
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
reverse(arr, arr.length);
}
}
```
### Output
```text
Reversed array is:
50
40
30
20
10
```
---
## Method 3: Using `Collections.reverse()`
If you prefer a cleaner, more declarative approach and are working with wrapper objects (like `Integer` instead of primitive `int`), you can leverage the Java Collections Framework.
### How It Works
* We convert the array into a list using `Arrays.asList(array)`.
* We pass this list to `Collections.reverse()`, which reverses the list in-place.
* Because `Arrays.asList()` returns a wrapper list backed by the original array, reversing the list also reverses the underlying array.
### Code Example
```java
import java.util.*;
public class ArrayReverseCollections {
/* Reverse the array using java.util.Arrays.asList() and Collections.reverse() */
static void reverse(Integer a[]) {
Collections.reverse(Arrays.asList(a));
System.out.println(Arrays.asList(a));
}
public static void main(String[] args) {
Integer[] arr = {10, 20, 30, 40, 50};
reverse(arr);
}
}
```
### Output
```text
[50, 40, 30, 20, 10]
```
---
## Summary & Considerations
| Method | Time Complexity | Space Complexity | Modifies Original Array? | Best Used For |
| :--- | :--- | :--- | :--- | :--- |
| **Auxiliary Array** | $O(N)$ | $O(N)$ | No | When you need to preserve the original array. |
| **In-Place Swap** | $O(N)$ | $O(1)$ | Yes | Memory-constrained environments and primitive arrays. |
| **Collections Framework** | $O(N)$ | $O(1)$ | Yes | Object arrays (`Integer[]`, `String[]`) where clean, readable code is preferred. |
### Key Takeaways
* **Primitive vs. Object Arrays**: `Collections.reverse()` only works with arrays of objects (e.g., `Integer[]`). If you have a primitive array (e.g., `int[]`), you must use either the manual swap method or convert it to its wrapper equivalent first.
* **Performance**: For large datasets, the **In-Place Swap** method is highly recommended as it avoids the overhead of object creation and garbage collection.
YouTip