2013-04-29 67 views
2

我在這裏有一個方法,搜索並打印我認爲是字典的所有字母組合的字符串形式輸入的節點。 我想指定每個打印輸出,並將其與我的搜索(字符串x)方法進行比較,以查找單詞是否在字典中。 我無法確定如何將每個字母組合指定爲一個字符串。我正在將它們打印出來,但不知道如何使該行打印字符串傳遞給搜索方法。如何從java中的trie中打印出發現的單詞?

public void print(Node node) { 

    if(node == null) return; 
    for(int i = 0; i < R; i++) { 
     if(node.next[i] != null) { 
      //printing each character 
      System.out.print((char) (97 + i)); 

      //identifying letter combo     
      if(node.next[i].isWord == true) { 

       //here i want to make that combo a string     
       System.out.println(); 
      } 
      print(node.next[i]); 
     } 

    } 
}  
+0

你試圖實現的是有點奇怪。 – 2013-04-29 14:40:09

+0

我沒有完全理解你的要求。但是,這可能會有所幫助。 'String s1; s1 = s1 +(char)(97 + i);' – 2013-04-29 14:43:33

回答

0

您可以使用 String s1 = ""; s1 = s1 + (char)(97+i);字符添加到字符串。您可以通過給你的search功能

東西,這樣一來,

初始化字符串來""外的for循環String s1 = ""

雖然循環不斷增加的字符,以String保持串

s1 = s1 + (char)(97+i); 

現在,

if(node.next[i].isWord == true) 
{ 
    //u can use the String here; 
      System.out.println(s1); 
} 
+0

你爲什麼用97添加'i'?什麼是97? – Hengameh 2015-09-22 09:50:30