字符串倒序输出
题目:将字符串abcdefg倒序输出(主意:不能用reverse函数)。方法一:利用String类的toCharArray(),再倒序输出数组。
1
public
class
javatest {
2 public static void main(String[] args) {
3 String originalString = " abcdefg " ;
4 String resultString = "" ;
5 char [] charArray = originalString.toCharArray();
6 for ( int i = charArray.length - 1 ; i >= 0 ; i -- ){
7 resultString += charArray[i];
8 }
9 System.out.println(resultString);
10 }
11 }
方法二:如果使用reverse()方法
2 public static void main(String[] args) {
3 String originalString = " abcdefg " ;
4 String resultString = "" ;
5 char [] charArray = originalString.toCharArray();
6 for ( int i = charArray.length - 1 ; i >= 0 ; i -- ){
7 resultString += charArray[i];
8 }
9 System.out.println(resultString);
10 }
11 }
1
2 public class reverseTest {
3
4 public static void main(String[] args) {
5 String originalString = " abcdefg " ;
6 StringBuffer stringBuffer = new StringBuffer(originalString);
7 System.out.println(stringBuffer.reverse());
8 }
9 }
10
2 public class reverseTest {
3
4 public static void main(String[] args) {
5 String originalString = " abcdefg " ;
6 StringBuffer stringBuffer = new StringBuffer(originalString);
7 System.out.println(stringBuffer.reverse());
8 }
9 }
10