Created
August 13, 2019 07:19
-
-
Save gauravkukade/c1cee5b55fa5ee1554b68c31797d2e33 to your computer and use it in GitHub Desktop.
A Java program to reverse a string. We are using 'reverse()' method of the Collections. This method reverse the order of elements of the list specified. The program is embedded at https://coderolls.com/reverse-a-string-in-java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Java program to reverse a string. | |
* | |
* We are using 'reverse()' method of the Collections. This method reverse the | |
* order of elements of the list specified. | |
* | |
* @author Gaurav Kukade at coderolls.com | |
*/ | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class ReverseStringUsingReeverseMethodOfCollections { | |
public static void main(String[] args) { | |
String blogName = "coderolls.com"; | |
char [] stringCharArray = blogName.toCharArray(); | |
List<Character> arrayList = new ArrayList<>(); | |
String reversedString = ""; | |
for(char ch : stringCharArray) { | |
arrayList.add(ch); | |
} | |
Collections.reverse(arrayList); | |
for( Character ch: arrayList) { | |
reversedString = reversedString + ch; | |
} | |
System.out.print("The reversed string of the '"+blogName+"' is: "); | |
System.out.println(reversedString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment