<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-28  評論-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!";
          }
    }


    哈希應用---字符統計

    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,

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

    正則可以在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方法分隔字符串時,分隔符如果用到一些特殊字符,可能會得不到我們預期的結果。

    我們看jdk doc中說明

    public String[] split(String regex)

     Splits 
    this string around matches of the given regular expression. 

    參數regex是一個 regular
    -expression的匹配模式而不是一個簡單的String,他對一些特殊的字符可能會出現你預想不到的結果,比如測試下面的代碼:

    用豎線 
    | 分隔字符串,你將得不到預期的結果

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

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

    用豎 
    * 分隔字符串運行將拋出java.util.regex.PatternSyntaxException異常,用加號 + 也是如此。

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

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

    顯然,
    + * 不是有效的模式匹配規則表達式,用"\\*" "\\+"轉義后即可得到正確的結果。

    "|" 分隔串時雖然能夠執行,但是卻不是預期的目的,"\\|"轉義后即可得到正確的結果。

    還有如果想在串中使用
    "\"字符,則也需要轉義.首先要表達"aaaa\bbbb"這個串就應該用"aaaa\\bbbb",如果要分隔就應該這樣才能得到正確結果:

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


    posted on 2007-11-04 12:55 fullfocus 閱讀(1418) 評論(0)  編輯  收藏 所屬分類: JAVA/J2EE
    主站蜘蛛池模板: 亚洲jizzjizz少妇| 亚洲jjzzjjzz在线观看| 美女被免费视频网站| 午夜免费福利影院| 亚洲综合一区无码精品| 噜噜嘿在线视频免费观看| 亚洲精品乱码久久久久久蜜桃图片| 国产精品爱啪在线线免费观看| 亚洲精品在线免费观看视频| 永久看日本大片免费35分钟| 亚洲国产美女视频| 免费看的一级毛片| 老司机福利在线免费观看| 亚洲精品一级无码中文字幕| a级毛片免费全部播放| 亚洲AV无码国产精品麻豆天美| 午夜免费福利片观看| 色偷偷女男人的天堂亚洲网| 国产黄色片在线免费观看| 一级全免费视频播放| 亚洲视频一区调教| 午夜一级免费视频| eeuss免费天堂影院| 久久亚洲中文字幕精品有坂深雪| 国产精品69白浆在线观看免费| 亚洲国产高清国产拍精品| 亚洲日韩国产成网在线观看| 日本免费久久久久久久网站| 亚洲一区二区久久| 亚洲国产成人久久一区WWW| 午夜精品免费在线观看| 亚洲欧美中文日韩视频| 日本亚洲欧洲免费天堂午夜看片女人员| 99久久免费中文字幕精品| 日韩国产欧美亚洲v片| 久久亚洲国产视频| 日韩高清在线免费看| 国产麻豆成人传媒免费观看| 2020年亚洲天天爽天天噜| 国产精品亚洲美女久久久| 亚州免费一级毛片|