如何自动生成一个arraylist?

时间:2014-02-05 09:42:38

标签: java

我有一个循环,它搜索字典,提取我已过滤的单词,然后将其放入arrayList中。然后我想要优化我的搜索并从这个arrayList中提取单词并将其放在另一个中。有没有生成arrayList的方法?通常我想做的事情我可能不知道我可能需要多少个数组列表。

我有一个手机键盘,每个号码分配3-4个字母。例如数字1(a,b,c)。我正在尝试编写一种方法,当我输入1234例如,我希望它能够在程序可以访问的单词库的帮助下预测我正在尝试编写的单词。所以,当我按下1时,我从库中提取所有以a,b或c开头的唯一单词。然后我考虑已被按下的数字2(d,e,f),并从前一个列表中提取所有以a,b,c开头但具有第二个字符d,e或f的单词。我正在做的是尝试生成新的arraylist来放入新的过滤词。我只是不知道如何自动生成创建arraylists。

我的示例代码:

//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("");
    }
}

2 个答案:

答案 0 :(得分:0)

如果您不知道所需的Arraylists数量,请创建ArrayLists的ArrayList,并将其命名为containerList。当您需要新的itemList时,创建新的ArrayList并将其添加到containerList。

答案 1 :(得分:0)

你真的使用了错误的数据结构。你需要的是一个看起来像这样的树结构(这个例子让我们使用“Tree”,“Three”和“Try”这两个词

                      T
                 H         R
                 R      E      Y
                 E      E
                 E

现在你可以向下走过树,如果他们已经输入了T和R那么你就可以做一个简单的树木行走并确定那棵树并试试可能的单词。一旦你知道要获得太多结果,你也应该中止树木行走。

此数据结构称为Trie:http://en.wikipedia.org/wiki/Trie