javaの3つの方法で文字列反転を実現します。


これは何も言いません。
package test;

import java.util.Stack;

//         (           ,      ,       )
public class CharReverse {	
	String string = new String("Hello world");
	int length = string.length();
	
	//  StringBuilder  reverse      ,    
	public void     () {		
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append(string);				
		System.out.println(sBuilder.reverse());
	}
	
	//       ,            
	public void     () {
		char[] string1 = new char[string.length()] ;
		for (int i = 0; i < length; i++) {
			string1 [(length-1) - i] = string.charAt(i);
		}
		System.out.println(string1);
	}
	
    //                
	public void    () {
		Stack sta = new Stack<>();
		for (int i = 0; i < length; i++) {
			sta.push(string.charAt(i)) ;
		}
		while (!sta.isEmpty()) {
			System.out.print(sta.pop());		
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CharReverse a1 = new CharReverse();
		a1.    ();
		a1.    ();
		a1.   ();
	}	
}
drow olleH
drow olleH
drow olleH