A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 马上都有 中级黑马   /  2014-4-16 17:05  /  1111 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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());
                }
        }
}
这是我的代码,你还有更好的方法吗?

4 个回复

倒序浏览
本帖最后由 曲佳奇 于 2014-4-16 17:31 编辑

StringBuffer不让用?
  1. class Demo {
  2.         public static void main(String[] args) {
  3.                   String str="abcde";
  4.                   StringBuffer s = new StringBuffer(str);
  5.                   s.reverse();
  6.                   System.out.println(s);
  7.         }
  8. }
复制代码
或者
  1. public class Fanzhuang
  2. {
  3.         public static void main(String[] args)
  4.         {         
  5.                 String str="abcde";
  6.                 System.out.println(resever(str));
  7.         }

  8.         public static String resever(String str)
  9.         {
  10.                  String s="";
  11.               for(int i=str.length()-1;i>=0;i--){
  12.                       s+=str.charAt(i);
  13.               }
  14.               return s;
  15.         }
  16. }
复制代码



回复 使用道具 举报
        public static String reverse1(String s) {  
         
                  int length = s.length();  //获取字符串的长度
                  String reverse = "";  
                  for (int i = length-1; i>=0; i--)  {
                           reverse =  reverse+s.charAt(i);//根据索引循环遍历字符串先将最后的取出。放在前面
                   }
                  return reverse;  

          }  
回复 使用道具 举报
package com.itheima;
import java.io.*;
import java.util.*;
public class reverse_char {
//这是我的 两种方法 实现
       
        public static void main(String []args)throws IOException
        {       
                System.out.println("请输入你要输入的字符");
                BufferedReader read_line=new BufferedReader(new InputStreamReader(System.in));
                String str=new String();
                while(true)
                {
                        str=read_line.readLine();
                        System.out.println("你输入的为"+str);
                        method_1(str);
                        method_2(str);
                }
               
               
        }
        public static void method_1(String str)
        {
               
                        String temp[]=str.split("\\s");
                        for(int i=temp.length-1;i>=0;i--)
                        {
                                System.out.println(temp[i]+"");
                        }
                       
                        for(int i=0;i<temp.length;i++)
                        {
                                System.out.println(temp[i]+"");
                        }
                       
        }
       
        public static void swap(char[] arr, int begin, int end) {
                while(begin < end) {
                        char temp = arr[begin];
                        arr[begin] = arr[end];
                        arr[end] = temp;
                        begin++;
                        end--;
                        }
       
        public static void method_2(String str) {
                char[] arr = str.toCharArray();
                swap(arr, 0, arr.length - 1);
                int begin = 0;
                for (int i = 1; i < arr.length; i++) {
                        if (arr[i] == ' ') {
                                swap(arr, begin, i - 1);
                                begin = i + 1;
                        }
                        for (int j = 1; j < arr.length; j++) {
                                System.out.println(arr[j]);
                       
                }

               
        }

}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马