Write a program to reverse a String Array in Java?

import java.util.Arrays;

public class ReverseStringArrayExample {

	public static void reverse(String[] inputArray) {

		System.out.println("Input array is : " + Arrays.toString(inputArray));

		// Return if array is null or empty
		if (inputArray == null || inputArray.length <= 1) {
			System.out.println("The input array is null or empty");
			return;
		} else {
			System.out.println("Size of  Array is: " + inputArray.length);
		}

		for (int i = 0; i < inputArray.length / 2; i++) {
			String temp = inputArray[i];
			// swap string
			inputArray[i] = inputArray[inputArray.length - 1 - i];
			inputArray[inputArray.length - 1 - i] = temp;
		}

		System.out.println("Reversed array is: " + Arrays.toString(inputArray));
	}


	public static void main(String[] args) {
	
		System.out.println("Reverse String Array  Example");
		
		String[] strings = { "Abc", "NMOP", "Stuvw","Xyz"};
		
		reverse(strings);
	}
}
Output
Reverse String Array Example Input array is : [Abc, NMOP, Stuvw, Xyz] Size of Array is: 4 Reversed array is: [Xyz, Stuvw, NMOP, Abc]










Related Examples


Write a program to check if an input String is Palindrome or not?
Calculate the factorial of a given number using Java?
How to reverse a number in Java?
Write a program to convert from decimal to binary?
Producer Consumer Example in Thread
Write a program to reverse a String?








Receive Latest Java Examples in Your Email: