Thursday, 12 January 2017

How to Reverse an Array in java

How to Reverse an Array in java
To simply Reverse an array in java you can follow our simple program. Here is the code for reversing an Array.

import java.io.*;
ReverseArray {

/*Function to reverse arr[] from start to end*/
static void reverseArray(int arr[], int start, int end)
{
       int temp;
       if(start >= end)
         return;
       temp = arr[start];
       arr[start] = arr[end];
       arr[end] = temp;
       reverseArray(arr, start+1, end-1);
}
/* Utility that prints out an array on a line */
static void printArray(int arr[], int size)
{
   int i;
   for (i=0; i < size; i++)
       System.out.println(arr[i] +" ");
     System.out.println("");
}
/* Driver function to check for above functions*/
public static void main (String[] args) {
       int arr[] = {1,2,3,4,5,6};
       printArray(arr,0,5);
       System.out.println("Reversed array is");
       printArray(arr, 6);
   }
}

Another Program to reverse an array in java Without using an additional array.


import java.util.Arrays;

public class ArrayReverseExample
{
    static void reverseArray(int inputArray[])
    {
        System.out.println("Array Before Reverse : "+Arrays.toString(inputArray));
       
        int temp;
       
        for (int i = 0; i < inputArray.length/2; i++)
        {
            temp = inputArray[i];
           
            inputArray[i] = inputArray[inputArray.length-1-i];
           
            inputArray[inputArray.length-1-i] = temp;
        }
       
        System.out.println("Array After Reverse : "+Arrays.toString(inputArray));
    }
   
    public static void main(String[] args)
    {  
        reverseArray(new int[]{4, 5, 8, 9, 10});
       
        System.out.println("...................................");
       
        reverseArray(new int[]{12, 9, 21, 17, 33, 7});
       
        System.out.println("...................................");
       
        reverseArray(new int[]{891, 569, 921, 187, 343, 476, 555});
    }  
}

OUTPUT OF THE PROGRAM

Array Before Reverse : [4, 5, 8, 9, 10]
Array After Reverse : [10, 9, 8, 5, 4]
..................................
Array Before Reverse : [12, 9, 21, 17, 33, 7]
Array After Reverse : [7, 33, 17, 21, 9, 12]
...................................
Array Before Reverse : [891, 569, 921, 187, 343, 476, 555]
Array After Reverse : [555, 476, 343, 187, 921, 569, 891]

Another Program


import java.util.ArrayList;
import java.util.Collections;

public class Main {
   public static void main(String[] args) {
      ArrayList arrayList = new ArrayList();
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
      arrayList.add("D");
      arrayList.add("E");
      System.out.println("Before Reverse Order: " + arrayList);
      Collections.reverse(arrayList);
      System.out.println("After Reverse Order: " + arrayList);
   }
}

if you like this program then let us know. Do a like and comment.

Other programs in java

 Java Program to display triangle 1 23 456 78910

Factorial In Java

2 comments:

Post a Comment