Here is a way to implement String inversion in 5 in Java.
1. Arrays implement String inversion
//Array implements String inversion public String reverseByArray(){ if(str == null || str.length() == 1){ return null; } char[] ch = str.toCharArray();//Convert a string into a character array for(int i = 0; i < ch.length/2 ; i++){ char temp = ch[i]; ch[i] = ch[ch.length-i-1]; ch[ch.length-i-1] = temp; } return new String(ch); }2. String inversion is implemented
//Use the stack to implement String inversion public String reverseByStack(){ if(str == null || str.length() == 1){ return null; } Stack<Character> stack = new Stack<Character>(); char[] ch = str.toCharArray();//Convert strings into character array for (char c : ch) { stack.push(c);//Each character, push the stack} for (int i = 0; i < ch.length; i++) { ch[i] = stack.pop();//Remove the top object of this stack} return new String(ch); } 3. Reverse traversal to realize String inversion
//Use reverse traversal to implement String inversion public String reverseBySort(){ if(str == null || str.length() == 1){ return null; } StringBuffer sb = new StringBuffer(); for (int i = str.length() -1 ; i >= 0; i--) { sb.append(str.charAt(i));//Use StringBuffer to splice characters from right to left} return sb.toString(); } 4. Bit operation realizes String reversal
//Use bit operation to implement String inversion public String reverseByBit() { if(str == null || str.length() == 1){ return null; } char[] ch = str.toCharArray();//Convert strings into character array int len = str.length(); for(int i= 0; i< len/ 2; i++) { ch[i]^= ch[len- 1- i]; ch[len- 1- i]^= ch[i]; ch[i]^= ch[len- 1- i]; } return new String(ch); } 5. Recursively implement String inversion
//Use recursion to implement String inversion public String reverseByRecursive(String str){ if(str == null || str.length() == 0){ return null; } if(str.length() == 1){ return str; } else { //Seave the string from the subscript of 1, return reverseByRecursive(str.substring(1)) + str.charAt(0); } } VI. Test
public class Test { public static void main(String[] args) { String s = "123456"; Reverse r = new Reverse(s); System.out.println(r.reverseByArray()); System.out.println(r.reverseByStack()); System.out.println(r.reverseBySort()); System.out.println(r.reverseByBit()); System.out.println(r.reverseByRecursive(s)); }}7. Results
8. All codes used for String inversion
public class Reverse { private String str = null; public Reverse(String str){ this.str = str; } //Array implementation String reverse public String reverseByArray(){ if(str == null || str.length() == 1){ return null; } char[] ch = str.toCharArray();//Convert string into a character array for(int i = 0; i < ch.length/2 ; i++){ char temp = ch[i]; ch[i] = ch[ch.length-i-1]; ch[ch.length-i-1] = temp; } return new String(ch); } //Use stack to implement String inversion public String reverseByStack(){ if(str == null || str.length() == 1){ return null; } Stack<Character> stack = new Stack<Character>(); char[] ch = str.toCharArray();//Convert strings into character array for (char c : ch) { stack.push(c);//Each character, push the stack} for (int i = 0; i < ch.length; i++) { ch[i] = stack.pop();//Remove the top object of this stack} return new String(ch); } //Use inverse traversal to implement String inversion public String reverseBySort(){ if(str == null || str.length() == 1){ return null; } StringBuffer sb = new StringBuffer(); for (int i = str.length() -1 ; i >= 0; i--) { sb.append(str.charAt(i));//Use StringBuffer to splice characters from right to left} return sb.toString(); } //Use bit operations to implement String inversion public String reverseByBit() { if(str == null || str.length() == 1){ return null; } char[] ch = str.toCharArray();//Convert strings into character array int len = str.length(); for(int i= 0; i< len/ 2; i++) { ch[i]^= ch[len- 1- i]; ch[len- 1- i]^= ch[i]; ch[i]^= ch[len- 1- i]; } return new String(ch); } //Use recursion to implement String reverseByRecursive(String str){ if(str == null || str.length() == 0){ return null; } if(str.length() == 1){ return str; } else { //Seave the string from the subscript of 1, return reverseByRecursive(str.substring(1)) + str.charAt(0); } }}The above 5 ways to implement String reversal in Java are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.