1. <tt id="5hhch"><source id="5hhch"></source></tt>
    1. <xmp id="5hhch"></xmp>

  2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

    <rp id="5hhch"></rp>
        <dfn id="5hhch"></dfn>

      1. Java常用字符串反轉的五種方案

        時間:2024-05-06 15:47:20 JAVA認證 我要投稿
        • 相關推薦

        Java常用字符串反轉的五種方案

          java程序中如何一個字符串進行反轉?或者在字符串中指定部分進行反轉的方法有哪些?為幫助大家解決這個疑問,yjbys小編為同學們分享最新Java常用字符串反轉的五種方案,快來看看吧!

        Java常用字符串反轉的五種方案

          Java常用字符串反轉的5種方案,代碼如下:

          第一個類是運行類Run.java

          package com.imooc.strreverse;

          import java.util.Scanner;

          public class Run {

          public Run() {

          System.out.print("請輸入一個字符串:");

          Scanner sc = new Scanner(System.in);

          String str = sc.next();

          System.out.println();

          String reversedStrByArray = new Reverse(str).reverseByArray();

          System.out.println("對應反轉字符串為[Array]:"+ reversedStrByArray);

          String reversedStrByStack = new Reverse(str).reverseByStack();

          System.out.println("對應反轉字符串為[Stack]:"+ reversedStrByStack);

          String reversedStrBySort = new Reverse(str).reverseBySort();

          System.out.println("對應反轉字符串為[逆序遍歷]:"+ reversedStrBySort);

          String reversedStrByBit = new Reverse(str).reverseByBit();

          System.out.println("對應反轉字符串為[位運算]:"+ reversedStrByBit);

          String reversedStrByRecursive = new Reverse(str).reverseByRecursive(str);

          System.out.println("對應反轉字符串為[遞歸]:"+ reversedStrByRecursive); }

          public static void main(String[] args) {

          new Run();

          }

          }

          第二段代碼是實現類Reverse.java

          package com.imooc.strreverse;

          import java.util.Stack;

          public class Reverse {

          String str = new String();

          public Reverse(String str) {

          this.str = str;

          }

          //用數組實現

          public String reverseByArray() {

          if(str == null str.length() == 0) {

          return str;

          }

          int len = str.length();

          char[] chArray = str.toCharArray();

          for(int i= 0; i< len/2; i++) {

          char temp;

          temp = chArray[i];

          chArray[i] = chArray[len- 1- i];

          chArray[len- 1- i] = temp;

          }

          return new String(chArray);

          }

          //用棧實現

          public String reverseByStack() {

          if(str == null str.length() == 0) {

          return str;

          }

          Stack strStack = new Stack();

          char[] chArray = str.toCharArray();

          for(Character ch: chArray) {

          strStack.push(ch);

          }

          int len = str.length();

          for(int i= 0; i< len; i++) { chArray[i] = strStack.pop();

          }

          return new String(chArray);

          }

          //用逆序遍歷實現

          public String reverseBySort() {

          if(str == null str.length() == 0) {

          return str;

          }

          StringBuilder sb = new StringBuilder();

          for(int i= str.length()- 1; i>= 0; i--) {

          sb.append(str.charAt(i));

          }

          return sb.toString();

          }

          //用位運算實現

          public String reverseByBit() {

          if(str == null str.length() == 0) {

          return str;

          }

          char[] chArray = str.toCharArray();

          int len = str.length();

          for(int i= 0; i< len/ 2; i++) {

          chArray[i]^= chArray[len- 1- i];

          chArray[len- 1- i]^= chArray[i];

          chArray[i]^= chArray[len- 1- i];

          }

          return new String(chArray);

          }

          //用遞歸實現

          public String reverseByRecursive(String str) {

          if(str == null str.length() == 0) {

          return str;

          }

          int len = str.length();

          if(len == 1) {

          return str;

          } else {

          return reverseByRecursive(str.substring(1))+ str.charAt(0);

          }

          }

          }

        【Java常用字符串反轉的五種方案】相關文章:

        關于JAVA字符串的拼接與性能06-07

        Java認證基礎知識:java字符串轉化整型問題07-21

        sun認證java關于字符串處理技巧06-01

        JAVA十六進制與字符串的轉換方法09-17

        反轉壽司的做法08-01

        javascript字符串對象常用api函數小結06-22

        反轉壽司的制作做法07-10

        Java枚舉的常用方法10-05

        Java數組定義常用方法09-28

        php開發中常用的操作字符串的函數說明07-19

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码

        1. <tt id="5hhch"><source id="5hhch"></source></tt>
          1. <xmp id="5hhch"></xmp>

        2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

          <rp id="5hhch"></rp>
              <dfn id="5hhch"></dfn>