posts - 8,  comments - 11,  trackbacks - 0

          import java.util.*;
          /*
           * @author  詩語江南
           * @function  統計字符串中的重復部分并整理輸出,
           *      我用了兩種方法來做.
           */
          public class StrShowTimes{
           public static void main(String[] r){
            String str = "帥哥,美女,帥哥,野獸,美女,帥哥";

            Map s1 = strTimesWithMap(str);
            Set keys = s1.keySet();
            Iterator it = keys.iterator();
            while(it.hasNext()){
             String key  = (String)it.next();
             int value = (Integer) s1.get(key);
             System.out.print(key + ": " + value+ ", ");
            }
            System.out.println();
            
            strTimesWithArray(str);
           }

           //使用HashMap的方法,該方法比較簡單
           public static Map strTimesWithMap(String str){
            //key: 子字符串 String , value: 重復次數 Integer
            Map strMap = new HashMap();
            String[] strArr = str.split(",");
            for(int i =0; i< strArr.length ; i++){
             String key = strArr[i] ;
             if(strMap.containsKey(key)){
              int value = (Integer) strMap.get(key);
              strMap.put(key,++value);
             }else{
              strMap.put(key,1);
             }   
            }  
            return strMap;
           }

           //使用雙數組的方法,一個字符串數組存字符串
           //一個整形數組存與字符串數組對應位置上的字符串出現的次數
           public static void strTimesWithArray(String str){
            String[] tempArr = str.split(",");
            int i , end = 0 , len = tempArr.length;
            String[] strArr = new String[len];
            int[] intArr = new int[len];
            boolean isChange ;
            for(i = 0 ; i < len ; i++){
             isChange = false;
             for(int j = 0 ; j < end ; j++){  
              if(tempArr[i].equals(strArr[j])){
               intArr[j] = intArr[j] +1 ;
               isChange = true;   
               break;
              }      
             }
             if(isChange) continue;
             strArr[end] = tempArr[i];
             intArr[end++] = 1; 
            }
            for(i = 0 ; i < end ; i++){
             System.out.print(strArr[i] + ": " + intArr[i] + " ");
            }
           }

          }

          posted on 2007-10-05 13:36 詩語江南 閱讀(1679) 評論(3)  編輯  收藏 所屬分類: Core JAVA

          FeedBack:
          # re: 統計字符串中的重復部分并整理輸出[未登錄]
          2007-10-05 21:37 | xmlspy
          這個東西如果自己研究研究還好,
          如果使用的話,加以用common-lang中的StringUtils.java
          當中大部分的String操作都有了  回復  更多評論
            
          # re: 統計字符串中的重復部分并整理輸出
          2008-11-24 19:04 | ff
          ff  回復  更多評論
            
          # re: 統計字符串中的重復部分并整理輸出
          2008-11-24 19:12 | hello3180
          小弟在瀏覽網頁時 突然看到閣下您寫的 統計字符串中的重復部分并整理輸出
          的java程序特別地興奮 十分期待能向閣下“取經”。QQ:137206889  回復  更多評論
            
          <2008年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          友情連接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 勃利县| 霞浦县| 科技| 清水县| 陆河县| 南康市| 遵义市| 鲁甸县| 祥云县| 同德县| 类乌齐县| 南宁市| 石棉县| 乐陵市| 舟曲县| 英德市| 沧源| 江门市| 甘德县| 平阴县| 昭觉县| 景谷| 梅河口市| 深圳市| 丽江市| 麦盖提县| 炎陵县| 南华县| 东乌| 通州区| 涞水县| 广平县| 阿拉善左旗| 怀柔区| 府谷县| 独山县| 三门峡市| 临朐县| 黑山县| 贵州省| 金乡县|