在此,我将单词存储在一个数组列表中,并将与其对应的相应电话号码存储在另一个数组列表中。我希望能够输入一个数字并返回另一个列表中对应的所有单词。我有这样的数组列表设置。
List<String> listWords = new ArrayList<String>(); // An ArrayList which stores all added words.
List<String> listNum = new ArrayList<String>();// An ArrayList which stores all phone numbers that correspond to all the added words
单词的转换就像在电话键盘上一样(即 2 = a,b,c 3 = d,e,f 等)。
我也像这样简单地添加单词;
public void readWords() 
{
    PhoneWords ph = new PhoneWords();
    try
    {
        // Open the file that is the first 
        // command line parameter
        FileInputStream fstream = new FileInputStream("words.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   
        {
            String phNum = ph.word2Num(strLine);
            listWords.add(position, strLine);
            listNum.add(position, phNum);
            position++; // index position, only used when initally adding the words
        }
    }catch (Exception e)
    {
        //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }