Powered by Blogger.

4 Ways to Reverse an Array in Java

>> Wednesday, August 20, 2014

Explain 'different ways to reverse an array' is one of the famous java interview questions. Using an array in programming is common with primitive types like e.g. int, long, double and String arrays to perform some operations. But to understand quickly we are using integer array with numbers to reverse. Actually we should know different types of arrays and how to iterate them. The basic programs on adding two matrices and multiplication of matrices is important .
four ways to reverse an array in java_JavabynataraJ
We can reverse an array in 4 different ways.
 They are:
  1. Reading array values from the last index
  2. Rotate half of its array length and swap values
  3. Use org.apache.commons.lang.ArrayUtils reverse method
  4. Use common swapping technique.
The first way to reverse an array:
1. Reading array values from the last index
If the Array length is 6, then read the array from the index 5 to print from the reverse. This is basic approach to print reverse of an array.
// First method to reverse an array
int arrnum[] = new int[] { 12, 10, 100, 67, 90 };
// print the original array
System.out.println("printing original array");
for (int i = 0; i < arrnum.length; i++) {
   System.out.println(arrnum[i]);
}
// Reading array values from the last index
System.out.println("printing Reversed Array: ");
for (int i = arrnum.length - 1; i >= 0; i--) {
   System.out.println(arrnum[i]);
}

Output:
90,67,100,10,12

2. Rotate half of its array length and swap values
The loop has to be rotated half of the length of the array times. For instance, if the length of the array is 4 then the loop has to be rotated 2 times,  if 11 then 5 times (you get 5.5 but rounded to 5).
// Second method to reverse an array
int arr[] = new int[] { 10, 20, 30, 50, 70,80 };
System.out.println("Third method to reverse an array:");
for (int i = 0; i < arr.length / 2; i++) {
   int temp = arr[i];
   arr[i] = arr[arr.length - i - 1];
   arr[arr.length - i - 1] = temp;
}
for (int i = 0; i < arr.length; i++) {
   System.out.println(arr[i]);
}
Output:
80,70,50,30,20,10

3. Use org.apache.commons.lang.ArrayUtils reverse method
This is Licensed to the Apache Software Foundation not from Oracle. ArrayUtils.reverse(array) reverses the order of the given array.
// Third method to reverse an array
int array[] = new int[] { 11, 13, 24, 36, 44, 66 };
//using  org.apache.commons.lang.ArrayUtils
ArrayUtils.reverse(array);
System.out.println("Using apache commons:" + array);
for (int i = 0; i < array.length; i++) {
   System.out.println(array[i]);
}
Output:
66,44,36,24,13,11

4. Using common swapping technique.
This is common swapping technique to reversing array elements
// Fourth method to reverse an Array
int data[] = new int[] { 10,15, 25, 35, 45, 55 };
System.out.println("Fourth method to reverse an array:");
int left = 0;
int right = data.length - 1;
while (left < right) {
   // swap the values at the left and right indices
   int temp = data[left];
   data[left] = data[right];
   data[right] = temp;
   // move the left and right index pointers in toward the center
   left++;
   right--;
}
for (int i = 0; i < data.length; i++) {
   System.out.println(data[i]);
}

Output:
55,45,35,25,15,10

Combining all the logics in a single program :

package com.javabynataraj;
import org.apache.commons.lang.ArrayUtils;
//http://javabynataraj.blogspot.com
public class ReverseArray {
 public static void main(String[] args) {
  
  int arrnum[] = new int[] { 12, 10, 100, 67, 90 };
  // print the original array
  System.out.println("printing original array");
  for (int i = 0; i < arrnum.length; i++) {
    System.out.println(arrnum[i]);
  }
  // Reading array values from the last index
  System.out.println("printing Reversed Array: ");
  for (int i = arrnum.length - 1; i >= 0; i--) {
    System.out.println(arrnum[i]);
  }

  // Second method to reverse an array
  int arr[] = new int[] { 10, 20, 30, 50, 70 };
  System.out.println("Second method:");
  System.out.println("Rotate half of its array length and swap values");
  for (int i = 0; i < arr.length / 2; i++) {
    int temp = arr[i];
    arr[i] = arr[arr.length - i - 1];
    arr[arr.length - i - 1] = temp;
  }
  for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
  }

  // Third method to reverse an array
  int array[] = new int[] { 11, 13, 24, 36, 44, 66 };
  System.out.println("Third method:Using apache commons");
  //using  org.apache.commons.lang.ArrayUtils
  ArrayUtils.reverse(array);
  for (int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
  }

  // Fourth method to reverse an Array
  int data[] = new int[] { 15, 25, 35, 45, 55 };
  System.out.println("Fourth method: swaping technique:");
  int left = 0;
  int right = data.length - 1;
  while (left < right) {
    // swap the values at the left and right indices
    int temp = data[left];
    data[left] = data[right];
    data[right] = temp;
    // move the left and right index pointers in toward the center
    left++;
    right--;
  }
  for (int i = 0; i < data.length; i++) {
    System.out.println(data[i]);
  }
 }
}
Run the above program and find the output.
please write your valuable comments if you find any bugs.

Reference Books:

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.