国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

利用java實現單詞倒序排列

2019-11-26 15:02:43
字體:
來源:轉載
供稿:網友

本文就是會將數組里面的單詞進行倒序排列 例如 how old are you -> you are old how

示例程序輸出結果:

the first:
How old are you !? I don't understand
the second:
understand don't I ?! you are old How

示例代碼    

public static void main(String[] args) {    char[] chars= new String("How old are you !? I don't understand").toCharArray();    System.out.println("the first:");    System.out.println(chars);         reverseWords(chars); //主要方法         System.out.println("the second:");    System.out.println(chars);  }      /**   * 會將數組里面的單詞 倒序排列 例如 how old are you -> you are old how   * @param chars   */  public static void reverseWords(char[] chars) {    reverseChars(chars,0,chars.length-1);    int begin = -1;    int end = 0;    for(int i=0;i<chars.length;i++){      char c = chars[i];      if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='/''){ //簡單的判斷了一下是否是連續的單詞        if(begin==-1){          begin = i;          end=i;        }else{          end=i;          if(i==chars.length-1){            reverseChars(chars,begin,end);          }        }      }else{        if(begin!=-1){          reverseChars(chars,begin,end);          begin=-1;          end=0;        }      }    }  }   /**   * 將char 一定范圍內的 字符 倒序排列 例如   hello -> olleh   * @param chars 數組   * @param begin 開始位置   * @param end  結束位置   */  public static void reverseChars(char[] chars, int begin, int end) {    while(end>begin){      char c = chars[begin];      chars[begin] = chars[end];      chars[end] = c;      begin++;      end--;    }  }

以上就是利用java實現單詞倒序排列,希望對大家能夠理解,對大家有所幫助

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玉树县| 金塔县| 平度市| 富川| 嘉荫县| 新津县| 寿阳县| 靖西县| 托克逊县| 文山县| 巴马| 江油市| 紫阳县| 日喀则市| 临西县| 西乌| 阳城县| 比如县| 光山县| 驻马店市| 德庆县| 德惠市| 吉首市| 红河县| 仁布县| 武义县| 汤阴县| 韶关市| 东平县| 普安县| 五台县| 贞丰县| 陆川县| 信丰县| 无锡市| 石柱| 平安县| 罗平县| 原平市| 鄂尔多斯市| 拉萨市|