Reverse an array or string

Reverse an array or string

How To Reverse Integer Or String Array In Java With Example

There are many ways we can perform how to reverse an array in java with example. Reversing an array is an simple question. Let understand the question through the example

Input    : {2, 4, 6, 8, 10}
Output : {10, 8, 6, 4, 2}

Algorithm to reverse an array using third variable :

  • Initialize an array with values
  • Swap the values of left indices with the right indices till the mid element.
  • print reverse array.

Code to Reverse int Array in Java

import java.util.*;
public class prepinsta
{
    public static void main (String[] args
    {  
        // Given input array 
        int[] inputArray = {2,4,6,8,10}; 
        // Print array before reverse  
        System.out.println(“Array without reverse” + 
                            Arrays.toString(inputArray)); 
        // Calling method to swap elements 
        reverse(inputArray);
    }
    public static void reverse(int[] inputArray) {
        for (int left = 0, right = inputArray.length – 1
                        left < right; left++, right–) {
            // swap the values at the left and right indices
            int temp = inputArray[left];
            inputArray[left]  = inputArray[right];
            inputArray[right] = temp;
        }
        // Printing the Array after reverse  
        System.out.print(“Reverse Array :”);
        for(int i : inputArray)
            System.out.print(” “+i);
    }
}
OUTPUT:

Array without reverse  [2, 4, 6, 8, 10]
Reverse Array : 10 8 6 4 2

Algorithm to Reverse String Array in Java

  • Convert the String array to the list using the Arrays.asList() method.
  •  Reverse the list using Collections.reverse() method
  •  Convert the list back to the array using the list.toArray() method.

 Code to Reverse String Array in Java

import java.util.*;
public class prepinsta
{
    public static void main (String[] args
    {  
        // Given input array 
        String[] inputArray = {“India”,“UK”,“PAKISTAN”,“Australia”}; 
        // Print array before reverse  
        System.out.println(“Array without reverse : “ + 
                            Arrays.toString(inputArray)); 
        // Calling method to swap elements 
        reverseArray(inputArray);
    }
   public static void reverseArray(String[] arr) {
        // Converting Array to List
        List<Stringlist = Arrays.asList(arr);
        // Reversing the list using Collections.reverse() method
        Collections.reverse(list);
        // Converting list back to Array
        String[] reversedArray = list.toArray(arr);
        // Printing the reverse Array
        System.out.print(“Reverse Array : “ + Arrays.toString(reversedArray));
   }
}
OUTPUT:

Array without reverse :  [India, UK, PAKISTAN, Australia]
Reverse Array :   [Australia, PAKISTAN, UK, India]

4 comments on “Reverse an array or string”


  • manali

    import java.util.Arrays;
    import java.util.Scanner;

    public class ReverseString {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter size of array:”);
    int size = sc.nextInt();
    int arr[]=new int[size];
    System.out.println(“Enter array:”);
    for (int i = 0; i =0;i–) {
    System.out.println(arr[i]);
    //System.out.println(Arrays.toString(arr));
    }
    }
    }


  • nilesh

    public class ArrayPrint {

    public static void main(String[] args) {

    int arr[]={10,20,45,75,8,2,6,12,8,7,5,1};

    System.out.println(“array element revers orders”);
    for (int k = arr.length-1;k>=0; k–) {

    System.out.println(arr[k]);

    }
    }

    }


    • nilesh

      public class ArrayPrint {

      public static void main(String[] args) {
      // TODO Auto-generated method stub

      String str2=”abc”;
      String revers=””;

      for (int i=str2.length()-1;i>=0; i–) {
      revers=revers+str2.charAt(i);
      }
      System.out.println(revers);

      }

      }