一、思路:將字符串轉化爲字符串數組,而後根據下標反向輸出
java
public void reverse() { String s = "hello"; char[] c; c = s.toCharArray(); //轉化爲字符串數組 for (int i = c.length - 1; i >= 0; i--) { System.out.print(c[i]); //下標逆向輸出 } }
二、用String的那個charAt()方法數組
public void reverse2() { String s = "hello"; for (int i = s.length()-1; i >= 0; --i) { System.out.print(s.charAt(i)); //下標逆向輸出 } }
三、調用StringBuffer的reverse()方法spa
public void reverse1() { StringBuffer s = new StringBuffer("hello"); System.out.println(s.reverse()); }
四、字符串兩邊互掉位置,從新組合造成新的字符串而後輸出code