本帖最后由 杨玉辉 于 2013-4-7 16:12 编辑
- package cn.itheima;
- import static java.util.Arrays.*;
- import java.util.Collections;
- import java.util.List;
- public class ReverseString {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //String s1 = "abcdefg";
- String s1 = "a kdeg3453464esfswe ";
-
-
- String[] sr = s1.split("");
- List<String> list = asList(sr);
- Collections.reverse(list);
- String[] arr = list.toArray(new String[list.size()]);
- String s3="";
- for(String s2: arr){
- System.out.print(s3.concat(s2));
- } }
- }
复制代码 将字符串切割,获取字符串数组存入List集合,结合Collections 工具类的reverse()方法,翻转后转换成字符串数组,然后,构造一个新的空字符串s3将数组元素以字符串形式添加进来,实现翻转功能- StringBuffer sb = new StringBuffer();
-
- sb.append(s1);
-
- System.out.println(sb.reverse());
-
复制代码 |
|