我的hashtable是全局的,我用了一个函数constructtable来建这个表,然后在collectwordpair的函数中get,但是get到的都是null, 而只是的table size却又显示里面是有东西的。在constructtalbe函数里,我也试过get,是可以正确的get出东西的。请高手给我看看:
 private static void ConstructTable(){
        String word = new String();
        StringBuffer WordChain = new StringBuffer();
        byte Aword[] = null;
        ResultSet rs = null;
        Statement stm1 = null;
        PreparedStatement stm2 = null;
        Connection con1 = null;
        String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        String DATABASE_URL = "jdbc:mysql://localhost/wordbase"; 
        int i = 0;
        try{
           
           Class.forName(JDBC_DRIVER);
           con1 = DriverManager.getConnection(DATABASE_URL, "root", "root");
           stm1 = con1.createStatement();
           //stm2 = con1.prepareStatement("SELECT * FROM worddatas where word = ?");
           //construct w_table
           rs = stm1.executeQuery("SELECT *  FROM worddatas");
           rs.beforeFirst();
           while(rs.next()){
               word = rs.getString(1);
               Aword = word.getBytes("ISO-8859-1");
               word = new String(Aword, "GB2312");
               
               //System.out.println(word);
               W_table.put(Aword, word);
               //System.out.println((String)W_table.get(Aword));
               //i++;
               
           }
            
          
           //System.out.println(i);
            con1.close();
       }
       
       catch(Exception e){
           System.out.println(e);
           System.exit(1);
       }      } 
    
    // collect word pair
    private static void CollectWordPair(String artical){
        int i, j, k, m, n;
        char Line[] = new char[8192];        
        File InputFile = null;
        FileInputStream fin = null;
        InputStreamReader InArtical = null;
        int NumberOfRead = -1;
        StringBuffer Sen = new StringBuffer();
        String GWord_InSen = new String();
        String AWord_InSen = new String();
        String GWord_InDat = new String();
        String AWord_InDat = new String();
        String Sentence = new String();
        String pair_insen = new String();
        String pair_indat = new String();
        Connection con1 = null;
        String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        String DATABASE_URL = "jdbc:mysql://localhost/wordbase"; 
        PreparedStatement pstm1 = null;
        PreparedStatement pstm2 = null;
        PreparedStatement pstm3 = null;
        ResultSet rs_current = null;
        int T = 0;
        byte word[] = null;
        
        try{
            //connect database
            Class.forName(JDBC_DRIVER);
            con1 = DriverManager.getConnection(DATABASE_URL, "root", "root");
            pstm1 = con1.prepareStatement("UPDATE Probability_c SET Times = ? WHERE G_String = ? AND A_String = ?");
            pstm2 = con1.prepareStatement("INSERT INTO Probability_c (G_String, A_String, Times) VALUES (?, ?, ?)");
            pstm3 = con1.prepareStatement("SELECT * FROM Probability_c WHERE G_String = ? AND A_String = ?");
            System.out.println("connect database success");
            
            
            InputFile = new File(artical);
            fin = new FileInputStream(InputFile);
            InArtical = new InputStreamReader(fin);
            
            
            
            do{
               NumberOfRead = InArtical.read(Line, 0, 8192);
               for(i=0; i<8192; i++){
                   if(Line[i] != '\n'){
                       Sen.append(Line[i]);
                   }
                   else{
                       System.out.println("sentence constructed");
                       Sentence = Sen.toString();
                       Sen.delete(0, Sen.length());
                       Sentence.trim();
                      
                       System.out.println("begin to search word pair");
                       for(j=0; j<Sentence.length(); j++){
                           for(m=5; m>0; m--){
                               if(j+m > Sentence.length()-2)continue;   // m to large,extended the sentence
                               
                               GWord_InSen = Sentence.substring(j,j+m);
                               word = GWord_InSen.getBytes("ISO-8859-1");
                               //T = W_table.size();
                               //System.out.println(T);
                               GWord_InDat = (String)W_table.get(word);
                               //System.out.println(GWord_InDat);
                               if(GWord_InDat != null){
                                   System.out.println("a given word found!");
                                   for(n=5; n>0; n--){
                                       if(j+m+n > Sentence.length()-1)continue;        // n to large,extended the sentence
                                       AWord_InSen = Sentence.substring(j+m, j+m+n);
                                       AWord_InDat = (String)W_table.get(GWord_InSen);
                                       if(AWord_InDat != null){
                                           System.out.println("an after word found");
                                           pstm3.clearParameters();
                                           pstm3.setString(1, GWord_InSen);
                                           pstm3.setString(2, AWord_InSen);
                                           rs_current = pstm3.executeQuery();
                                           rs_current.first();
                                           if(rs_current.getRow() != 0){
                                              System.out.println("pair exist");
                                              T = rs_current.getInt(3) + 1;
                                              pstm1.clearParameters();
                                              pstm1.setInt(1, T);
                                              pstm1.setString(2, GWord_InSen);
                                              pstm1.setString(3, AWord_InSen);
                                              pstm1.execute();
                                              }
                                          else{
                                              System.out.println("pair not exist");
                                              pstm2.clearParameters();
                                              pstm2.setString(1, GWord_InSen);
                                              pstm2.setString(2, AWord_InSen);
                                              pstm2.setInt(3, 1);
                                              pstm2.execute();
                                              }
                                               
                                           }
                                       }
                                       
                                   }
                               }
                               
                           }
                       }
                   }
                              
            }while(NumberOfRead == 8192);
            
            con1.close();   
        }
        catch(Exception e){
            System.out.println(e);
            System.exit(1);
        }
    }

解决方案 »

  1.   

    Aword = word.getBytes("ISO-8859-1");word = GWord_InSen.getBytes("ISO-8859-1");你自己加打印看下这两个东西相同吗
      

  2.   

    Aword 和word不一定是相同的,我的本意是通过get(word)来查找,如果get(word) != null就表示找到,否则就是找不到
      

  3.   

    用个简单的例子说明一下你这个问题吧
    看如下代码
    Hashtable temp = new Hashtable();
    temp.put("aa".getBytes(),"bb");
    System.err.println((String)temp.get("aa".getBytes()));
    为什么输出的结果是null?原因: 在put中,用"aa".getBytes()的值做KEY,相当于用一个byte[]类型的对像来做KEY值,那么这个KEY 就是这个对像的hashCode的值.
    而对于byte[]数组来说,hashCode的值是跟数组本身的内容没有关系的.这个值是根据该对像的地址来计算的.
    所以,在第2次使用"aa".getBytes()时,得到的对像跟第一次是不同的(虽然内容都是相同的),所以,取出来是null看下的例子,可以看出来,abyte和bbyte虽说内容相同,但hashCode不同,但abyte的内容修改后,他的hashCode是不变的
    public static void main(String[] args) {
    byte[] abyte = new byte[]{'1','2'};
    byte[] bbyte = new byte[]{'1','2'};
    System.err.println("abyte hash code:"+abyte.hashCode());
    System.err.println("bbyte hash code"+bbyte.hashCode());

    abyte[0]='3';
    abyte[1]='4';
    System.err.println("abyte hash code after change:"+abyte.hashCode());
    }