Java program to reverse an array

Reversing an array in java is often wiped out by three simple methods.

Examples:

Input : 1, 2, 3, 4, 5
Output :5, 4, 3, 2, 1

Input :  10, 20, 30, 40
Output : 40, 30, 20, 10

The first method is as follows:

(i) Take input the dimensions of the array and therefore the elements of the array.
(ii) Consider a function reverse which takes the parameters-the array(say arr) and therefore the size of the array(say n).
(iii) Inside the function, a replacement array (with the array size of the primary array, arr) is initialized. The array arr[] is iterated from the primary element and every element of array arr[] is placed within the new array from the rear, i.e, the new array is iterated from its last element.
(iv) during this way, all the weather of the array arr[] are placed reversely within the new array.
(v) Further, we will iterate through the new array from the start and print the weather of the array.

/* Basic Java program that reverses an array*/

public class reverseArray {

	/* function that reverses array and stores it
	in another array*/
	static void reverse(int a[], int n)
	{
		int[] b = new int[n];
		int j = n;
		for (int i = 0; i < n; i++) {
			b[j - 1] = a[i];
			j = j - 1;
		}

		/*printing the reversed array*/
		System.out.println("Reversed array is: \n");
		for (int k = 0; k < n; k++) {
			System.out.println(b[k]);
		}
	}

	public static void main(String[] args)
	{
		int [] arr = {10, 20, 30, 40, 50};
		reverse(arr, arr.length);
	}
}

Output:

Reversed array is: 

50
40
30
20
10

The second method uses a similar code for the inputting and printing of the array. However, we don’t create a replacement array just like the above method. Instead, we reverse the first array itself. during this method, we swap the weather of the array. the primary element is swapped with the last element. The second element id swapped with the last but one element than on.
For instance, consider array [1, 2, 3, …., n-2, n-1, n]. We swap 1 with n, 2 with n-1, 3 with n-2 and further.

/* Program that reverses array in less number of swaps*/

public class arrayReverse {

	/*function swaps the array's first element with last element,
	second element with last second element and so on*/
	static void reverse(int a[], int n)
	{
		int i, k, t;
		for (i = 0; i < n / 2; i++) {
			t = a[i];
			a[i] = a[n - i - 1];
			a[n - i - 1] = t;
		}

		/*printing the reversed array*/
		System.out.println("Reversed array is: \n");
		for (k = 0; k < n; k++) {
			System.out.println(a[k]);
		}
	}

	public static void main(String[] args)
	{
		int [] arr = {10, 20, 30, 40, 50};
		reverse(arr, arr.length);
	}
}

Output:

Reversed array is: 

50
40
30
20
10
 
// Reversing an array using Java collections.
import java.util.*;

public class reversingArray {

	/*function reverses the elements of the array*/
	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:

[50, 40, 30, 20, 10]

 

Submit Your Programming Assignment Details