`

interview-----str----倒序输出

 
阅读更多
方法一:利用String类的toCharArray(),再倒序输出数组。
 public class javatest { 
    public static void main(String[] args) { 
       String originalString = "abcdefg"; 
       String resultString = ""; 
       char[] charArray = originalString.toCharArray(); 
       for (int i = charArray.length-1; i>=0; i--){ 
          resultString +=charArray[i]; 
       } 
       System.out.println(resultString); 
    } 
}

方法二:
如果使用reverse()方法
public class reverseTest { 
    
    public static void main(String[] args) { 
         String originalString = "abcdefg"; 
          StringBuffer stringBuffer = new StringBuffer(originalString); 
          System.out.println(stringBuffer.reverse()); 
   } 
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics