Let’s learn reverse string array in java.
Reverse string array in java
Here’s the reverse string array program using for loop.
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
Reversing a string array using Arrays.asList() method
To reverse a string array we can use ArrayList.asList() method.
import java.util.Arrays; import java.util.Collections; public class ReverseStringArrayUsingFunction { // function to reverse elements of string array static void reverseString(String myArray[]) { Collections.reverse(Arrays.asList(myArray)); System.out.println("Reversed String Array : " + Arrays.asList(myArray)); } public static void main(String[] args) { String[] myArray = {"eleven", "twelve", "thirteen", "fourteen", "fifteen"}; System.out.println("Given Array: " + Arrays.asList(myArray)); reverseString(myArray); } }
Output:
Given Array: [eleven, twelve, thirteen, fourteen, fifteen]
Reversed String Array : [fifteen, fourteen, thirteen, twelve, eleven]
Reverse array in java using Collections.reverse() method
Collections.reverse() method reverses the order of the elements in the specified list. 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.reverse() method.
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.
Also read – java overview