简体   繁体   中英

How to generate an arraylist automatically?

I have a Loop that searches a dictionary, extracts the words that I have filtered and then puts it in an arrayList. I then want to refine my search and extract words from this arrayList and place it in another one. Is there a method for generating an arrayList? often what I am trying to do I might not know exactly how many arrayLists I might need.

i have a mobile phone keyboard with 3-4 letters assigned per number. for example number 1(a,b,c). I am trying to write a method that when i input 1234 for example, i would like it to predict the word that i am trying to write, with the help of a library of words that the program has access to. so, when i press 1, i extract from the library all unique words starting with a,b,or c. I then consider number 2 (d,e,f) that has be pressed , and extract from the previous list all words starting with a,b,c but has as a second character d,e,or f. What I am doing is trying to generate new arraylist to put the new filtered words in. I just don't know how to automate the generation of creating arraylists.

my example code:

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

If you don't know the number of Arraylists you need, make an ArrayList of ArrayLists, lets name it containerList. When you need a new itemList, create new ArrayList and add it to the containerList.

You are using the wrong data structure for this really. What you need is a tree structure that looks like this (for this example lets use the words "Tree", "Three" and "Try"

                      T
                 H         R
                 R      E      Y
                 E      E
                 E

Now you can just walk down through the tree, if they have already typed T and R then you can just do a simple treewalk and determine that tree and try are the possible words. You should also abort the treewalk as soon as you know you are going to get too many results.

This data structure is called a Trie: http://en.wikipedia.org/wiki/Trie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM