Java中5種方式實現String反轉_java

來源:互聯網
上載者:User

這裡介紹Java中5中實現String反轉的方式。

一、數組實現String反轉

//數組實現String反轉  public String reverseByArray(){    if(str == null || str.length() == 1){      return null;    }    char[] ch = str.toCharArray();//字串轉換成字元數組    for(int i = 0 ; i < ch.length/2 ; i++){      char temp = ch[i];      ch[i] = ch[ch.length-i-1];      ch[ch.length-i-1] = temp;    }    return new String(ch);  }

二、棧實現String反轉

//用棧實現String反轉  public String reverseByStack(){    if(str == null || str.length() == 1){      return null;    }    Stack<Character> stack = new Stack<Character>();    char[] ch = str.toCharArray();//字串轉換成字元數組    for (char c : ch) {      stack.push(c);//每個字元,推進棧    }    for (int i = 0; i < ch.length; i++) {      ch[i] = stack.pop();//移除這個堆棧的頂部對象    }    return new String(ch);  }

三、逆序遍曆實現String反轉

//用逆序遍曆實現String反轉  public String reverseBySort(){    if(str == null || str.length() == 1){      return null;    }    StringBuffer sb = new StringBuffer();    for (int i = str.length() -1 ; i >= 0; i--) {      sb.append(str.charAt(i));//使用StringBuffer從右往左拼接字元    }    return sb.toString();  }

四、位元運算實現String反轉

//使用位元運算實現String反轉  public String reverseByBit() {     if(str == null || str.length() == 1){      return null;    }    char[] ch = str.toCharArray();//字串轉換成字元數組    int len = str.length();     for(int i= 0; i< len/ 2; i++) {       ch[i]^= ch[len- 1- i];       ch[len- 1- i]^= ch[i];       ch[i]^= ch[len- 1- i];     }    return new String(ch);  }

五、遞迴實現String反轉

//使用遞迴實現String反轉  public String reverseByRecursive(String str){    if(str == null || str.length() == 0){      return null;    }    if(str.length() == 1){      return str;    } else {      //從下標為1開始截取字串,在返回下標為0的字元      return reverseByRecursive(str.substring(1)) + str.charAt(0);    }  }

六、測試

public class Test {  public static void main(String[] args) {    String s = "123456";    Reverse r = new Reverse(s);    System.out.println(r.reverseByArray());    System.out.println(r.reverseByStack());    System.out.println(r.reverseBySort());    System.out.println(r.reverseByBit());    System.out.println(r.reverseByRecursive(s));      }}

七、結果

八、用於String反轉的全部代碼

public class Reverse {  private String str = null;    public Reverse(String str){    this.str = str;  }    //數組實現String反轉  public String reverseByArray(){    if(str == null || str.length() == 1){      return null;    }    char[] ch = str.toCharArray();//字串轉換成字元數組    for(int i = 0 ; i < ch.length/2 ; i++){      char temp = ch[i];      ch[i] = ch[ch.length-i-1];      ch[ch.length-i-1] = temp;    }    return new String(ch);  }  //用棧實現String反轉  public String reverseByStack(){    if(str == null || str.length() == 1){      return null;    }    Stack<Character> stack = new Stack<Character>();    char[] ch = str.toCharArray();//字串轉換成字元數組    for (char c : ch) {      stack.push(c);//每個字元,推進棧    }    for (int i = 0; i < ch.length; i++) {      ch[i] = stack.pop();//移除這個堆棧的頂部對象    }    return new String(ch);  }  //用逆序遍曆實現String反轉  public String reverseBySort(){    if(str == null || str.length() == 1){      return null;    }    StringBuffer sb = new StringBuffer();    for (int i = str.length() -1 ; i >= 0; i--) {      sb.append(str.charAt(i));//使用StringBuffer從右往左拼接字元    }    return sb.toString();  }  //使用位元運算實現String反轉  public String reverseByBit() {     if(str == null || str.length() == 1){      return null;    }    char[] ch = str.toCharArray();//字串轉換成字元數組    int len = str.length();     for(int i= 0; i< len/ 2; i++) {       ch[i]^= ch[len- 1- i];       ch[len- 1- i]^= ch[i];       ch[i]^= ch[len- 1- i];     }    return new String(ch);  }  //使用遞迴實現String反轉  public String reverseByRecursive(String str){    if(str == null || str.length() == 0){      return null;    }    if(str.length() == 1){      return str;    } else {      //從下標為1開始截取字串,在返回下標為0的字元      return reverseByRecursive(str.substring(1)) + str.charAt(0);    }  }}  

以上這篇Java中5種方式實現String反轉就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.