2014-02-05 115 views
1

我有一個循環搜索字典,提取我已經過濾的單詞,然後將它放入一個arrayList。然後我想優化我的搜索並從這個arrayList中提取單詞,並將它放在另一個單詞中。有沒有一種方法來生成一個arrayList?通常我想要做的事情我可能不知道我可能需要多少個arrayLists。如何自動生成數組列表?

我有一個手機鍵盤,每個編號分配3-4個字母。例如編號1(a,b,c)。我試圖編寫一個方法,例如當我輸入1234時,我希望它能夠通過程序可以訪問的單詞庫的幫助來預測我正在嘗試編寫的單詞。所以,當我按1時,我從庫中提取所有以a,b或c開頭的獨特單詞。然後我考慮已經被按下的數字2(d,e,f),並從前面的列表中提取以a,b,c開始的所有單詞,但是具有作爲第二個字符d,e或f的所有單詞。我正在做的是試圖產生新的數組列表來放置新的過濾詞。我只是不知道如何自動創建數組列表的生成。

我的示例代碼:

//cycles through each possible alaphabetical characters represented by the numbers that constitute "signature" 
for (int countSignature = 0; countSignature < signature.length();countSignature++){ 
    if (signature.codePointAt(countSignature) == 50) {// Representing 2 
     for (int j = 0; j < set1.size(); j++){ //iterating through the array for each word that starts with a,b, or c. 
      if (arraySet1[j].codePointAt(countSignature) == 97 
        || arraySet1[j].codePointAt(countSignature) == 98 
        || arraySet1[j].codePointAt(countSignature) == 99) { 

       arrayListSet2.add(arraySet1[j]); 
       //need to generate new arrayList for the next round of filtering. 
       f(j++) 
      } 
     } 
    }else if(signature.codePointAt(countSignature) == 51){ // Representing 3 
     for (int j = 0; j < set1.size(); j++) { 
      if (arraySet1[j].codePointAt(countSignature) == 100 
        || arraySet1[j].codePointAt(countSignature) == 101 
        || arraySet1[j].codePointAt(countSignature) == 102) { 

       arrayListSet2.add(arraySet1[j]); 
      } 
     } 
    }else if(signature.codePointAt(countSignature) == 52){ // Representing 4 
     for (int j = 0; j < set1.size(); j++) { 
      if (arraySet1[j].codePointAt(countSignature) == 103 
        || arraySet1[j].codePointAt(countSignature) == 104 
        || arraySet1[j].codePointAt(countSignature) == 105) { 

       arrayListSet2.add(arraySet1[j]); 
      } 
     } 
    }else if(signature.codePointAt(countSignature) == 53){ // Representing 5 
     for (int j = 0; j < set1.size(); j++) { 
      if (arraySet1[j].codePointAt(countSignature) == 106 
        || arraySet1[j].codePointAt(countSignature) == 107 
        || arraySet1[j].codePointAt(countSignature) == 108) { 

       arrayListSet2.add(arraySet1[j]); 
      } 
     } 
    }else if(signature.codePointAt(countSignature) == 54){ // Representing 6 
     for (int j = 0; j < set1.size(); j++) { 
      if (arraySet1[j].codePointAt(countSignature) == 109 
        || arraySet1[j].codePointAt(countSignature) == 110 
        || arraySet1[j].codePointAt(countSignature) == 111) { 

       arrayListSet2.add(arraySet1[j]); 
      } 
     } 
    }else if(signature.codePointAt(countSignature) == 55){ // Representing 7 
     for (int j = 0; j < set1.size(); j++) { 
      if (arraySet1[j].codePointAt(countSignature) == 112 
        || arraySet1[j].codePointAt(countSignature) == 113 
        || arraySet1[j].codePointAt(countSignature) == 114 
        || arraySet1[j].codePointAt(countSignature) == 115){ 

       arrayListSet2.add(arraySet1[j]); 
      } 
     } 
    }else if(signature.codePointAt(countSignature) == 56){ // Representing 8 
     for (int j = 0; j < set1.size(); j++) { 
      if (arraySet1[j].codePointAt(countSignature) == 116 
        || arraySet1[j].codePointAt(countSignature) == 117 
        || arraySet1[j].codePointAt(countSignature) == 118) { 

       arrayListSet2.add(arraySet1[j]); 
      } 
     } 
    }else if(signature.codePointAt(countSignature) == 57){ // Representing 9 
     for (int j = 0; j < set1.size(); j++) { 
      if (arraySet1[j].codePointAt(countSignature) == 119 
        || arraySet1[j].codePointAt(countSignature) == 120 
        || arraySet1[j].codePointAt(countSignature) == 121 
        || arraySet1[j].codePointAt(countSignature) == 122) { 

       arrayListSet2.add(arraySet1[j]); 
      } 
     } 
    }else{ 
     System.out.println(""); 
    } 
} 
+0

你可能想重新考慮你的邏輯.. – Maroun

+0

邏輯不清楚。你需要更多的這個。 – Ajit

+0

http://stackoverflow.com/questions/1480663/how-can-i-slice-an-arraylist-out-of-an-arraylist-in-java – Rossiar

回答

0

如果你不知道你需要的ArrayList的數量,使的ArrayList的ArrayList,讓我們將其命名爲containerList。當你需要一個新的itemList時,創建新的ArrayList並將其添加到containerList。

0

你真的使用了錯誤的數據結構。你需要的是一個樹狀結構,看起來像這樣(在這個例子中可以使用的話「樹」,「三」和「嘗試」

     T 
       H   R 
       R  E  Y 
       E  E 
       E 

現在你可以走在樹,如果他們有已經鍵入了T和R,那麼你可以做一個簡單的treewalk,並確定樹和嘗試是可能的單詞,你應該也知道你會得到太多的結果後立即放棄樹木行道

該數據結構被稱爲Trie:http://en.wikipedia.org/wiki/Trie