隨筆-28  評(píng)論-51  文章-10  trackbacks-0
          hash哈希

          重寫hashCode()和equals()
          import java.util.*

          public class Hashs {
               
          public static void main(String[] args){
                     HashMap
          <Element,Figureout> h2=new HashMap<Element,Figureout>();
                    
          for(int i=0;i<10;i++)
                          h2.put(
          new Element(i), new Figureout());
                     System.out.println(
          "h2:");
                     System.out.println(
          "Get the result for Element:");
                     Element test
          =new Element(3);
                    
          if(h2.containsKey(test))
                          System.out.println((Figureout)h2.get(test));
                    
          else
                          System.out.println(
          "Not found");
                }
          }
          class Element{
               
          int number;
               
          public Element(int n){
                     number
          =n;
                } 
               
          public int hashCode()
               {
                  
          return number; 
               }
               
          public boolean equals(Object o)
               {
                   
          return (o instanceof Element)&&( number ==((Element)o).number);
                      
               }
          }

          class Figureout{
                Random r
          =new Random();
               
          boolean possible=r.nextDouble()>0.5;
               
          public String toString(){
                    
          if(possible)
                         
          return "OK!";
                    
          else
                         
          return "Impossible!";
                }
          }


          哈希應(yīng)用---字符統(tǒng)計(jì)

          import java.util.HashMap;

          public class CountWords {
              
          public static void count(String target) {
                  String[] array 
          = target.split(" ");//以空格來分隔

                  HashMap
          <String,Integer> map = new HashMap<String,Integer>();
                  
          for (String ss : array) {
                      
          if (map.containsKey(ss)) {
                          map.put(ss, map.get(ss)
          +1);
                      } 
          else {
                          map.put(ss, 
          1);
                      }
                  }
                  System.out.println(map);
              }
              
          public static void main(String[] args) {
                  String testString 
          = "kuikui is good man! yes ! kuikui is good man .";
                  CountWords.count(testString);
              }

          }

          字符串和正則 
          主要有StreamTokenizer, String.split,StringTokenizer,

          前兩者可以使用正則作為參數(shù),后者只能用直接分隔符作為參數(shù)

          正則可以在jdk中找到

          (http://hi.baidu.com/ecgql/blog/item/f176882b0c66affbe6cd40b5.html
          http://www.javaeye.com/subject/Regular-Expression),

          也可以使用開源包Jakarta-ORO(http://www.ccw.com.cn/htm/app/aprog/01_7_31_4.asp)

          特別地:
          在使用String.split方法分隔字符串時(shí),分隔符如果用到一些特殊字符,可能會(huì)得不到我們預(yù)期的結(jié)果。

          我們看jdk doc中說明

          public String[] split(String regex)

           Splits 
          this string around matches of the given regular expression. 

          參數(shù)regex是一個(gè) regular
          -expression的匹配模式而不是一個(gè)簡(jiǎn)單的String,他對(duì)一些特殊的字符可能會(huì)出現(xiàn)你預(yù)想不到的結(jié)果,比如測(cè)試下面的代碼:

          用豎線 
          | 分隔字符串,你將得不到預(yù)期的結(jié)果

              String[] aa 
          = "aaa|bbb|ccc".split("|");
              
          //String[] aa = "aaa|bbb|ccc".split("\\|"); 這樣才能得到正確的結(jié)果

              
          for (int i = 0 ; i <aa.length ; i++ ) {
                System.out.println(
          "--"+aa[i]);
              }

          用豎 
          * 分隔字符串運(yùn)行將拋出java.util.regex.PatternSyntaxException異常,用加號(hào) + 也是如此。

              String[] aa 
          = "aaa*bbb*ccc".split("*");
              
          //String[] aa = "aaa|bbb|ccc".split("\\*"); 這樣才能得到正確的結(jié)果   

              
          for (int i = 0 ; i <aa.length ; i++ ) {
                System.out.println(
          "--"+aa[i]);
              }

          顯然,
          + * 不是有效的模式匹配規(guī)則表達(dá)式,用"\\*" "\\+"轉(zhuǎn)義后即可得到正確的結(jié)果。

          "|" 分隔串時(shí)雖然能夠執(zhí)行,但是卻不是預(yù)期的目的,"\\|"轉(zhuǎn)義后即可得到正確的結(jié)果。

          還有如果想在串中使用
          "\"字符,則也需要轉(zhuǎn)義.首先要表達(dá)"aaaa\bbbb"這個(gè)串就應(yīng)該用"aaaa\\bbbb",如果要分隔就應(yīng)該這樣才能得到正確結(jié)果:

          String[] aa 
          = "aaa\\bbb\\bccc".split("\\\\");


          posted on 2007-11-04 12:55 fullfocus 閱讀(1429) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA/J2EE
          主站蜘蛛池模板: 青神县| 瓦房店市| 花莲县| 福泉市| 兴义市| 墨竹工卡县| 城市| 青川县| 上虞市| 盘锦市| 永修县| 平塘县| 吴堡县| 禄劝| 保德县| 株洲市| 宁国市| 芜湖县| 黑河市| 凉城县| 通辽市| 肇源县| 镇江市| 万全县| 台南市| 岫岩| 咸阳市| 思南县| 方城县| 成都市| 长岭县| 神池县| 抚松县| 乳山市| 白山市| 弋阳县| 中山市| 乐业县| 乐山市| 温泉县| 卢湾区|