Reverse an Array in Java

Reverse an array is a common and fundamental problem in programming, often used to build a strong understanding of array manipulation and algorithm design. This article explores different methods in Java, from simple approaches like using temporary arrays to advanced techniques like recursion and inbuilt methods, catering to learners at all levels.

1. Reverse Array Using Temporary Array

This method involves creating a new array (temporary array) to store the elements of the original array in reverse order. The original array is traversed from the last element to the first, and the elements are assigned to the new array sequentially.

  • Pros:
    • Simple to implement.
  • Cons:
    • Requires additional memory for the temporary array, which increases the space complexity.

Java Program

package com.javacodepoint.array;

//reverse an array using temporary array
public class ReverseArrayExample1 {

	public static void main(String[] args) {
		int[] arr = { 1, 2, 3, 4, 5 };
		int[] temp = new int[arr.length];

		for (int i = 0; i < arr.length; i++) {
			temp[i] = arr[arr.length - 1 - i];
		}

		System.out.println("Reversed array:");
		for (int num : temp) {
			System.out.print(num + " ");
		}
	}
}

2. Reverse Array Using Two Pointers

This method uses two pointers: one starting from the beginning of the array and the other from the end. The elements at these pointers are swapped, and the pointers are moved closer until they meet.

  • Pros:
    • Easy to understand and implement.
    • Space-efficient as it doesn’t require additional arrays.
  • Cons:
    • Requires careful pointer management to avoid errors.

Java Program

package com.javacodepoint.array;

//reverse an array using Two Pointers
public class ReverseArrayExample2 {

	public static void main(String[] args) {
		int[] arr = { 1, 2, 3, 4, 5 };

		int start = 0, end = arr.length - 1;
		while (start < end) {
			int temp = arr[start];
			arr[start] = arr[end];
			arr[end] = temp;
			start++;
			end--;
		}

		System.out.println("Reversed array:");
		for (int num : arr) {
			System.out.print(num + " ");
		}
	}
}

3. Reverse Array By Swapping Elements

This is a variation of the two-pointer approach. The array is iterated only up to its middle, and the elements at symmetric positions are swapped. It avoids using extra memory, making it space-efficient.

  • Pros:
    • Efficient in terms of both time and space.
  • Cons:
    • Not suitable for immutable data structures.

Java Program

package com.javacodepoint.array;

//reverse an array by swapping elements
public class ReverseArrayExample3 {

	public static void main(String[] args) {
		int[] arr = { 1, 2, 3, 4, 5 };

		for (int i = 0; i < arr.length / 2; i++) {
			int temp = arr[i];
			arr[i] = arr[arr.length - 1 - i];
			arr[arr.length - 1 - i] = temp;
		}

		System.out.println("Reversed array:");
		for (int num : arr) {
			System.out.print(num + " ");
		}
	}
}

4. Reverse Array Using Recursion

Recursion is a technique where the method calls itself to reverse the array. The first and last elements are swapped, and the function recurses on the remaining sub-array (excluding the first and last elements) until the base condition is met.

  • Pros:
    • Elegant and concise implementation.
    • Useful for understanding recursive concepts.
  • Cons:
    • Can cause stack overflow for large arrays due to deep recursion.
    • Slightly slower due to the overhead of function calls.

Java Program

package com.javacodepoint.array;

//reverse an array using Recursion
public class ReverseArrayExample4 {
	
	public static void reverse(int[] arr, int start, int end) {
		if (start >= end) {
			return;
		}

		int temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;

		reverse(arr, start + 1, end - 1);
	}

	public static void main(String[] args) {
		int[] arr = { 1, 2, 3, 4, 5 };

		reverse(arr, 0, arr.length - 1);

		System.out.println("Reversed array:");
		for (int num : arr) {
			System.out.print(num + " ");
		}
	}
}

5. Reverse Array Using Inbuilt Methods (Collections.reverse)

This method leverages Java’s inbuilt Collections.reverse() method. The array is first converted to a list, and then the reverse() method is applied to reverse the order of elements in the list.

  • Pros:
    • Quick and uses predefined methods.
    • Reduces the need for manual implementation.
  • Cons:
    • Works only with objects (e.g., Integer[]) and not primitive arrays directly (e.g., int[]).
    • It is slightly more complex if dealing with primitive arrays due to conversion.

Java Program

package com.javacodepoint.array;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

//reverse an array using inbuilt methods (Collections.reverse)
public class ReverseArrayExample5 {

	public static void main(String[] args) {
		Integer[] arr = { 1, 2, 3, 4, 5 };

		// Convert the array to a list and reverse it
		List<Integer> list = Arrays.asList(arr);
		Collections.reverse(list);

		System.out.println("Reversed array:");
		for (int num : list) {
			System.out.print(num + " ");
		}
	}
}

Java logical programs list


Java Basic Programs

Java String Programs

Java String Array Programs

Java Miscellaneous Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *