Best Way To Reverse String Array In Java

Sharing is healthy !!

Today we are going to demonstrate the best way to reverse string array in java.

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. Here in the below example we will be using the latter one,

Example: Best Way To Reverse String Array In Java

Also Read – Char Array to String Java Program

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

public class Reverse_String_Array {

public static void main(String[] args) {
 
String str_Hierarchy[] = new String[]{"Junior Developer","Senior Developer","Team Lead","Project Manager","Senior Manager","CEO"};
List< String> li = Arrays.asList(str_Hierarchy);
 
// Here we use reverse method of Collections class

Collections.reverse(li);
str_Hierarchy = (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 < str_Hierarchy.length;x++)
{
System.out.println(str_Hierarchy[x]);
}
}
}

Output:

Best Way To Reverse String Array In Java

Sharing is healthy !!

Leave a Reply

Your email address will not be published.