Java reverse string array

Let’s learn java reverse string array.

Java reverse string array

Here’s reverse string array program.

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"};
      System.out.println("Given string array: " + Arrays.toString(strHierarchy));
      for(int a = 0; a < strHierarchy.length / 2; a++)
      {
         String strTemp = strHierarchy[a];
         strHierarchy[a] = strHierarchy[strHierarchy.length - a - 1];
         strHierarchy[strHierarchy.length - a - 1] = strTemp;
      }
      System.out.println("Reversed string array: ");
      for(int a = 0; a < strHierarchy.length; a++)
      {
         System.out.println(strHierarchy[a]);
      }
   }
}


Output:

Given string array: [Junior Developer, Senior Developer, Team Lead, Project Manager, Senior Manager, CEO]
Reversed string array:
CEO
Senior Manager
Project Manager
Team Lead
Senior Developer
Junior Developer


Also read – program to reverse a string in java


reverse array in java using for loop

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

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


Also read – reverse a string in java using for loop


reverse an array without using another array in java

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

Here’s the java program to reverse an array without using another array.

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]


Also read – how to reverse a string in java word by word


reverse array in java using collections

Collections.reverse() method reverses the order of the elements in the specified list.

Also read – reverse a string in java using tochararray

reverse() method throws UnsupportedOperationException if the specified list or its list-iterator does not support the set operation.

Now let’s see an example on reverse array in java using collections.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ReverseStringArrayUsingCollections
{
   public static void main(String[] args)
   {
      // creating list of strings
      List<String> li = new ArrayList<String>();
      li.add("java");
      li.add("core");
      li.add("world");
      li.add("hello");
      System.out.println("Given list: " + li);
      Collections.reverse(li);
      System.out.println("After using collections: " + li);
   }
}


Output:

Given list: [java, core, world, hello]
After using collections: [hello, world, core, java]


Arrays class do not have reverse() method. So to reverse an array use Collections.reverse() method.

import java.util.Arrays;
import java.util.Collections;
public class ReverseArrayUsingCollections
{
   public static void main(String[] args)
   {
      Integer[] arr = {2, 4, 6, 8, 10};
      System.out.println("Given array: " + Arrays.toString(arr));
      Collections.reverse(Arrays.asList(arr));
      System.out.println("After using collections: " + Arrays.toString(arr));
   }
}


Output:

Given array: [2, 4, 6, 8, 10]
After using collections: [10, 8, 6, 4, 2]


So this is all about how to reverse a string array. I hope you have understood the concept of reversing a string array in java.