Java reverse string array

Let’s learn java reverse string array.

Java reverse string array

To reverse a string array in java, Collections.reverse() method is used. This method reverses the order of the elements in the specified list. Its syntax,

public static void reverse(List<?> list)

list – the list whose elements are to be reversed.

Here’s the java program,

import java.util.Collections;
import java.util.List;
import java.util.Arrays;

public class ReverseStringArrayInJava
{
   public static void main(String[] args)
   {
      String strHierarchy[] = new String[]{"Junior Developer","Senior Developer","Team Lead","Project Manager","Senior Manager","CEO"};
      List< String> li = Arrays.asList(strHierarchy);
      // Here we use reverse method of Collections class
      Collections.reverse(li);
      strHierarchy = (String[])li.toArray();
      System.out.println("Reversed string array : ");
      for(int x = 0; x < strHierarchy.length; x++)
      {
         System.out.println(strHierarchy[x]);
      }
   }
}



Output:

java reverse string array

Now let’s learn another way in java reverse string array example,

import java.util.Arrays;

public class ArrayUtilsReverse 
{
   public static void main(String[] args) 
   {
      String strArray[] = { "weekend.", "the", "enjoy", "let's", "Come" };
      System.out.println("Before : " + Arrays.toString(strArray));
      for(int a = 0; a < strArray.length / 2; a++)
      {
         String str = strArray[a];
         strArray[a] = strArray[strArray.length - a - 1];
         strArray[strArray.length - a - 1] = str; 
      }
      System.out.println("Reversed array : ");
      for(int a = 0; a < strArray.length; a++)
      {
         System.out.println(strArray[a]);
      }
   }
}



Output:

Before : [weekend., the, enjoy, let’s, Come]
Reversed array :
Come
let’s
enjoy
the
weekend.


Also read – program to reverse a string in java

using for loop

Let’s learn to reverse array in java using for loop. Here’s the java program,

public class ReverseArrayDemo 
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = new int[]{2, 4, 6, 8, 10};  
      System.out.println("Given array: ");  
      for(int a = 0; a < arrNumbers.length; a++)
      {
         System.out.print(arrNumbers[a] + " ");
      }
      System.out.println("Reverse array: ");
      // looping array in reverse order
      for(int a = arrNumbers.length - 1; a >= 0; a--) 
      {  
         System.out.print(arrNumbers[a] + " ");  
      }
   }
}



Output:

Given array:
2 4 6 8 10
Reverse array:
10 8 6 4 2

reverse a string using for loop

Now let’s learn reverse a string in java using for loop,

import java.util.Scanner;

public class ReverseStringDemo 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter a string to reverse: ");
      String strGiven = sc.nextLine();    
      System.out.println("Reverse a string in java using for loop '" + strGiven + "' is  : "); 
      for(int a = strGiven.length(); a > 0; --a)
      {
         System.out.print(strGiven.charAt(a - 1)); 
      }
      sc.close();
   }
}



Output:

Please enter a string to reverse: virat
Reverse a string in java using for loop ‘virat’ is : tariv

without using another array

Here we are going to reverse an array without using another array in java,

import java.util.Arrays;

public class ReverseWithoutArray 
{
   public int[] reverseArray(int[] arrNum)
   {
      if(arrNum == null || arrNum.length <= 1)
      {
         System.out.println("Invalid array.");
      }
      for(int a = 0; a < arrNum.length / 2; a++) 
      {
         int temp = arrNum[a];
         arrNum[a] = arrNum[arrNum.length - 1 - a];
         arrNum[arrNum.length - 1 - a] = temp;
      }
      return arrNum;
   }

   public static void main(String[] args) 
   {
      ReverseWithoutArray obj = new ReverseWithoutArray();
      int[] arrNumbers = {2, 4, 6, 8, 10, 12, 14, 16};
      System.out.println("Given array: " + Arrays.toString(arrNumbers));
      System.out.println("Reverse an array without using another array: " + Arrays.toString(obj.reverseArray(arrNumbers)));
   }
}



Output:

Given array: [2, 4, 6, 8, 10, 12, 14, 16]
Reverse an array without using another array: [16, 14, 12, 10, 8, 6, 4, 2]

word by word

To reverse given string word by word we are using split() method which returns array of strings computed by splitting this string around matches of the given regular expression.

Let’s see how to reverse a string in java word by word,

import java.util.Scanner;

public class ReverseWordByWord 
{
   public static String reverseString(String strInput)
   {
      String[] strWords = strInput.split("\s");
      String strOutput = "";
      for(int a = strWords.length-1; a >= 0; a--)
      {
         strOutput = strOutput + strWords[a] + " ";
      }
      return strOutput;
   }

   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter string to reverse word by word: ");
      String input = sc.nextLine();
      String output = reverseString(input);
      System.out.println("Given string: " + input);
      System.out.println("Reverse a string in java word by word: " + output);
      sc.close();
   }
}



Output:

Please enter string to reverse word by word: flower brackets
Given string: flower brackets
Reverse a string in java word by word: brackets flower

Using inbuilt function

Here we are going to learn how to reverse an array in java using inbuilt function,

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ReverseAnArray 
{
   public static void main(String[] args) 
   {
      String[] players = {"Mahendra Singh Dhoni", "Virat Kohli", "Rohit Sharma"};
      System.out.println("Before reverse: " + Arrays.toString(players) );
      List<String> listPlayers = Arrays.asList(players);      
      Collections.reverse(listPlayers);
      String[] strReversed = listPlayers.toArray(players);
      System.out.println("After reverse array: " + Arrays.toString(strReversed));
   }
}



Output:

Before reverse: [Mahendra Singh Dhoni, Virat Kohli, Rohit Sharma]
After reverse array: [Rohit Sharma, Virat Kohli, Mahendra Singh Dhoni]

char array java

Now let’s learn reverse char array java using toCharArray() method. Using for loop we print reverse of char array java. Here’s the java program,

public class ReverseCharArray 
{
   public static void main(String[] args) 
   {
      String str = "FlowerBrackets";
      // here we are converting
      // String to character array using toCharArray 
      char[] ch = str.toCharArray();
      for(int a = ch.length-1; a >= 0; a--)
      {
         System.out.print(ch[a]);
      }
   }
}



Output:

stekcarBrewolF