Java Reverse String Array

Hey guys!! Welcome to flower brackets blog. Today we are going to demonstrate java reverse string array.

Java Reverse String Array

To reverse a string in java the best way there are two methods, one using temporary array and swap them, second, using arrays and collections classes.

Also Read – 2 Best Ways To Reverse A String In Java

Here in the below example we will be using the latter one,

Example : java reverse string array

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

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

public class ReverseStringArray
{
   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("Best Way To Reverse String Array In Java\n");
      System.out.println("String Array has been reversed!!!\n");
 
      for(int x = 0; x < strHierarchy.length; x++)
      {
         System.out.println(strHierarchy[x]);
      }
   }
}

Output:

java reverse string array


Java reverse string array

Now let’s learn java reverse string array or how to reverse string array in java,

import java.util.Arrays;

public class ArrayUtilsReverse 
{
   public static void main(String[] args) 
   {
      String strArray[] = { "weekend.", "the", "enjoy", "let's", "Come" };
      System.out.println("Print string array in reverse order java : " + 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:

Print string array in reverse order java : [weekend., the, enjoy, let’s, Come]
Reversed array :
Come
let’s
enjoy
the
weekend.


conclusion

That’s it guys. So this is all about how to reverse a string array in java or reverse an array in java.

I hope you guys have understood the concept of reversing a string in java. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like