public class Fanzhuang { public static void main(String[] args) { String str="abcde"; resever(str); } public static void resever(String str) { //将字符串转换成字符数组 char[] ch=str.toCharArray(); //定义ArrayList集合 ArrayList<Character> al=new ArrayList<Character>(); //将字符串中的元素逆序添加到ArrayList集合 for(int x=0;x<ch.length;x++) { al.add(ch[ch.length-1-x]); } //遍历ArrayList集合,并打印 Iterator<Character> it=al.iterator(); while(it.hasNext()) { System.out.print(it.next()); } } } 这是我的代码,你还有更好的方法吗? |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |