Java array interview program to reverse an array

The blog provides the technique to reverse an array in java . “How to reverse an array” or “how to reverse an array list using collections” are the commonly asked interview questions for java developers for exam and technical interview preparation.

Problem Statement: how to reverse an array in java

Array can be reversed either using the array iteration or using Collections.reverse(ArrayList) method. The below given java array program provide the example for both approaches.

Reverse an Array using iteration:

  • Retrieve the Array Count from User Input
  • Retrieve the Array Elements from User Input
  • Retrieve the Array Search Element
  • Iterate the Array Element by Array length /2
  • Assign the array at 1st position to temp
  • Assign the array at last position to Assign the array at 1st position
  • Assign temp with the array at last position
  • Perform all iterations and elements will be reversed
package com.java.planforexams.array;
import java.util.Scanner;
public class ArrayReverseUsingIteration {
	public static void main(String[] args) {
		// Scanner object for reading User Input
		Scanner scan = new Scanner(System.in);
		int count;
		int searchElement ;
		// Receive the Array Element Count from User
		System.out.print("Provide the Array Element Count : ");
		// Get the Array Count
		count = scan.nextInt();
		//Set the Array Size with Count value
		int arrayInputFromUser[] = new int[count];
		// Receive the Array Elements from User.
		System.out.println("Enter the elements equal to array count:");
		// Iterate the array elements
		for (int incr=0; incr<count; incr++)
		{
			arrayInputFromUser[incr] = scan.nextInt();
		}
		 for (int i = 0; i < count / 2; i++) {
	         int temp = arrayInputFromUser[i];
	         arrayInputFromUser[i] = arrayInputFromUser[count-1 - i];
	         arrayInputFromUser[count - 1 - i] = temp;
	      } 
	      System.out.println("\n Reversed Array Output ::::::");
	      for (int i = 0; i < count; i++) {
	         System.out.print(arrayInputFromUser[i] + " ");
	      } 
	}
}
Output:
Provide the Array Element Count : 5
Enter the elements equal to array count:
 1
4
5
3
5

 Reversed Array Output ::::::
5 3 5 4 1 

Reverse an ArrayList using Collections.reverse(ArrayList)

  • Retrieve the Array Count from User Input
  • Retrieve the Array Elements from User Input store the value into ArrayList
  • Invoke Collections.reverse(arrayInputFromUserList) by passing the arrayList
package com.java.planforexams.array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class ReverseArrayList {
	public static void main(String[] args) {
		// Scanner object for reading User Input
		Scanner scan = new Scanner(System.in);
		int count;
		int searchElement ;
		 ArrayList arrayInputFromUserList = new ArrayList();
		// Receive the Array Element Count from User
		System.out.print("Provide the Array Element Count : ");
		// Get the Array Count
		count = scan.nextInt();
		//Set the Array Size with Count value
		// Receive the Array Elements from User.
		System.out.println("Enter the elements equal to array count:");
		// Iterate the array elements
		for (int incr=0; incr<count; incr++)
		{
			arrayInputFromUserList.add(scan.nextInt()) ;
		}
		System.out.println("Before Reverse Order: " + arrayInputFromUserList);
	     Collections.reverse(arrayInputFromUserList);
	     System.out.println("After Reverse Order: " + arrayInputFromUserList);
	}
}
Output:
Provide the Array Element Count : 5
Enter the elements equal to array count:
12
34
34
56
78
Before Reverse Order: [12, 34, 34, 56, 78]
After Reverse Order: [78, 56, 34, 34, 12]

Leave a Reply

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

*