How to Reverse a String Array in Java


In this example we will show how to reverse an array of String in Java.

Source Code

package com.beginner.examples;

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

public class ReverseStringArray {

	public static void main(String[] args) {
		
		//Create String array.
		String[] stringNames = new String[]{"Alice", "Bob", "Jack"}; 

		//first create a list from String array.
		List listString = Arrays.asList(stringNames);

		//next, reverse the list using Collections.reverse method.
		Collections.reverse(listString);
		
		stringNames = (String[]) listString.toArray();
		
		System.out.println("String array reversed result is ");
		for(int i=0; i < stringNames.length; i++){
			System.out.println(stringNames[i]);
		}
	}

}

Output:

String array reversed result is 
Jack
Bob
Alice

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments