最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用java實(shí)現(xiàn)單詞倒序排列

 更新時(shí)間:2015年07月30日 16:15:25   作者:Ds_Diamond  
這篇文章就是利用java實(shí)現(xiàn)單詞倒序排列,感覺像是在變魔術(shù),感興趣的小伙伴來(lái)見證一下

本文就是會(huì)將數(shù)組里面的單詞進(jìn)行倒序排列 例如 how old are you -> you are old how

示例程序輸出結(jié)果:

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);
  }
 
   
  /**
   * 會(huì)將數(shù)組里面的單詞 倒序排列 例如 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=='\''){ //簡(jiǎn)單的判斷了一下是否是連續(xù)的單詞
        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 一定范圍內(nèi)的 字符 倒序排列 例如   hello -> olleh
   * @param chars 數(shù)組
   * @param begin 開始位置
   * @param end  結(jié)束位置
   */
  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實(shí)現(xiàn)單詞倒序排列,希望對(duì)大家能夠理解,對(duì)大家有所幫助

相關(guān)文章

最新評(píng)論

砚山县| 宿州市| 图片| 荆州市| 大厂| 河间市| 巢湖市| 京山县| 合阳县| 库尔勒市| 广饶县| 惠东县| 琼结县| 腾冲县| 成武县| 拜泉县| 麻阳| 岑巩县| 泗阳县| 隆尧县| 左贡县| 河南省| 庆安县| 高碑店市| 昭苏县| 阳新县| 尉犁县| 乐安县| 祁门县| 泾源县| 准格尔旗| 黄大仙区| 安阳市| 新营市| 修水县| 岗巴县| 武义县| 宜川县| 板桥市| 富蕴县| 黄大仙区|